如何通过脚本使 Texture2D 可读

How to make Texture2D Readable via script

我想让用户能够解码从图库加载的 QR 图像,我找到了一个插件来探索图像并将其加载为 texture2D,但是要解码该 QR 码,Texture2D 必须是 readable/writable,我检查了插件,因为 Android 它使用 jar 进行探索和加载,在 IOS 平台中它使用打包的库,所以我无法访问代码图书馆,

我已经找到了答案,大多数解决方案是在 Unity inspector 中更改纹理的导入设置,但是由于这是代码加载的纹理,因此没有可用的 inspector 设置,所以我的问题是:

有没有办法通过代码制作这个加载的纹理read/writeable?无需访问库代码?

谢谢

这里是可以通过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));
}

如你所见,我试过了但是没有成功。

这是 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] 

:

来源 Texture2D 来自插件,我无法将其设置为 Read/Write 从编辑器启用或使用编辑器的 TextureImporter.isReadable 变量。

有两种方法可以做到这一点:

1。使用RenderTexture(推荐):

使用RenderTexture。使用 Graphics.Blit 将源 Texture2D 放入 RenderTexture 然后使用 Texture2D.ReadPixels 将图像从 RenderTexture 读取到新的 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;
}

用法:

Texture2D copy = duplicateTexture(sourceTextFromPlugin);

这应该可以工作并且不会引发任何错误。


2.使用Texture2D.GetRawTextureData() + Texture2D.LoadRawTextureData():

您不能使用 GetPixels32(),因为 Texture2D 不可读。您非常接近使用 GetRawTextureData().

您使用 Texture2D.LoadImage()GetRawTextureData() 加载失败。

Texture2D.LoadImage() 用于加载PNG/JPG数组字节而不是Texture2D数组字节。

如果您使用 Texture2D.GetRawTextureData() 阅读,则必须使用 Texture2D.LoadRawTextureData() 而不是 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;
}

上面的代码在编辑器中不会出现错误,但在独立构建中应该会出现错误。此外,即使在独立构建中出现错误,它仍然可以工作。我认为那个错误更像是一个警告。

我建议您使用方法 #1 来执行此操作,因为它不会引发任何错误。