How to make Texture2D readable via script

I want the user to be able to decode a QR image downloaded from the gallery, I found a plugin to explore and load an image as a texture2D, but to decode this QR code, Texture2D must be read / write, And I checked the plugin, for Android it does research and download files with jar and on IOS platform using packaged library, so I don't have access to lib code,

I was looking for an answer, most of the solutions was to change the texture import setting in the Unity inspector, but since this is a texture loaded by code, there is no inspector setting for that. So my question is:

Is there a way to make this loaded texture read / writeable by code? without having to access lib code?

thank

Here is the code that could get the texture this plugin

void OnImageLoad(string imgPath, Texture2D tex, ImageAndVideoPicker.ImageOrientation imgOrientation)
{
    Debug.Log("Image Location : " + imgPath);
    Debug.Log("Image Loaded : " + imgPath);
    texture = tex;
    Texture2D readableText = new Texture2D(tex.width, tex.height);
    readableText.LoadImage(tex.GetRawTextureData());

    string url = QRCodeDecodeController.DecodeByStaticPic(readableText);
    StartCoroutine(GetSceneAndLoadLevel(url));
}

      

As you can see, I tried this answer But no luck.

And here is the error shown by Android:

06-23 21:47:32.853: I/Unity(10557): (Filename: D Line: 0)
06-23 21:47:33.784: E/Unity(10557): Texture needs to be marked as Read/Write to be able to GetRawTextureData in player
06-23 21:47:33.784: E/Unity(10557): UnityEngine.Texture2D:GetRawTextureData()
06-23 21:47:33.784: E/Unity(10557): TestQR:OnImageLoad(String, Texture2D, ImageOrientation) (at D:\Unity Projects\nnkp\Assets\Scripts\QR\TestQR.cs:123)
06-23 21:47:33.784: E/Unity(10557): <LoadImage>c__Iterator0:MoveNext()
06-23 21:47:33.784: E/Unity(10557): UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
06-23 21:47:33.784: E/Unity(10557): [./artifacts/generated/common/runtime/TextureBindings.gen.cpp line 512] 

      

Note

The source Texture2D

comes from a plugin, I cannot set it to Read / Write Enabled from the editor or use the Editor variable TextureImporter.isReadable

.

+3


source to share


1 answer


There are two ways to do this:

1 . Use RenderTexture

(recommended):

Use RenderTexture

. Place the source Texture2D

in RenderTexture

with Graphics.Blit

, then use Texture2D.ReadPixels

to read the image from RenderTexture

in the new one Texture2D

.

Texture2D duplicateTexture(Texture2D source)
{
    RenderTexture renderTex = RenderTexture.GetTemporary(
                source.width,
                source.height,
                0,
                RenderTextureFormat.Default,
                RenderTextureReadWrite.Linear);

    Graphics.Blit(source, renderTex);
    RenderTexture previous = RenderTexture.active;
    RenderTexture.active = renderTex;
    Texture2D readableText = new Texture2D(source.width, source.height);
    readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
    readableText.Apply();
    RenderTexture.active = previous;
    RenderTexture.ReleaseTemporary(renderTex);
    return readableText;
}

      

Using

Texture2D copy = duplicateTexture(sourceTextFromPlugin);

      

This should work and shouldn't throw any errors.




2 . Use Texture2D.GetRawTextureData()

+ Texture2D.LoadRawTextureData()

:

You cannot use GetPixels32()

it because it is Texture2D

not readable. You were so close to using it GetRawTextureData()

.

Failed if you used Texture2D.LoadImage()

to download from GetRawTextureData()

.

Texture2D.LoadImage()

only used to load the bytes of a PNG / JPG array, not a Texture2D array.

If you are reading with Texture2D.GetRawTextureData()

, you should write with Texture2D.LoadRawTextureData()

not Texture2D.LoadImage()

.

Texture2D duplicateTexture(Texture2D source)
{
    byte[] pix = source.GetRawTextureData();
    Texture2D readableText = new Texture2D(source.width, source.height, source.format, false);
    readableText.LoadRawTextureData(pix);
    readableText.Apply();
    return readableText;
}

      

There will be no errors in the editor with the code above, but there should be an error in the offline assembly. Also, it should work even with an error in offline build. I think the error is more like a warning.

I recommend using method # 1 so that it doesn't throw errors.

+5


source







All Articles