如何通过脚本使 Texture2D 可读 [英] How to make Texture2D Readable via script

查看:44
本文介绍了如何通过脚本使 Texture2D 可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户能够解码从图库加载的 QR 图像,我找到了一个插件来探索和加载图像作为 Texture2D,但要解码该 QR 码,Texture2D 必须是可读/可写的,我检查了插件,对于 Android,它使用 jar 进行探索和加载,而在 IOS 平台中,它使用打包的库,所以我无法访问 lib 的代码,

I want to make user able to decode the QR image loaded from the gallery, I have found a plugin to explore and load the image as a texture2D, but to decode that QR code, the Texture2D has to be readable/writable, And I checked the plugin, for Android it's doing the exploring and loading stuff with a jar and in IOS platform it's using a packaged library, so I have no access to the code of the lib,

我已经搜索了答案,最解决方案是在Unity检查器中更改纹理的导入设置,但由于这是代码加载的纹理,因此没有可用的检查器设置,所以我的问题是:

I have searched for the answer, the most solution was to change the importing setting of texture in the Unity inspector, but since this is a texture loaded by code, there is not an inspector setting available for that, So my question is:

有没有办法让这个加载的纹理可以通过代码读/写?无需访问 lib 代码?

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

谢谢

这里是通过这个插件获取纹理的代码

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 have tried this answer But got no luck.

这是Android显示的错误:

And here is the error that showed 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
nkpAssetsScriptsQRTestQR.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 来自插件,我无法将其设置为从编辑器启用读/写或使用编辑器的 TextureImporter.isReadable 变量.

The source Texture2D is coming from a plugin, I can't set it to Read/Write Enabled from the Editor or use the Editor's TextureImporter.isReadable variable.

推荐答案

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

There are two ways to do this:

1.使用 RenderTexture(推荐):

使用RenderTexture.使用Graphics.Blit 将源Texture2D 放入RenderTexture 中,然后使用Texture2D.ReadPixels 读取图像>RenderTexture 到新的 Texture2D.

Use RenderTexture. Put the source Texture2D into RenderTexture with Graphics.Blit then use Texture2D.ReadPixels to read the image from RenderTexture into the new 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);

这应该可以工作并且不会抛出任何错误.

This should work and should not throw any error.

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

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

You can't use GetPixels32() because the Texture2D is not readable. You were so close about using GetRawTextureData().

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

You failed when you used Texture2D.LoadImage() to load from GetRawTextureData().

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

Texture2D.LoadImage() is only used to load PNG/JPG array bytes not Texture2D array byte.

如果您使用Texture2D.GetRawTextureData() 读取,则必须使用Texture2D.LoadRawTextureData() 而非Texture2D.LoadImage() 写入.

If you read with Texture2D.GetRawTextureData(), you must 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 error with the code above in the Editor but there should be an error in standalone build. Besides, it should still work even with the error in the standalone build. I think that error is more like a warning.

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

I recommend you use method #1 to do this as it will not throw any error.

这篇关于如何通过脚本使 Texture2D 可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆