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

查看:291
本文介绍了如何通过脚本使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:

是否有任何方法可以使已加载的纹理可由代码读取/写入?无需访问库代码?

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

谢谢

以下是可以通过此插件获取纹理的代码>

Here is the code that could get the texture by 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 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\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来自插件,我无法从编辑器中将其设置为读/写已启用",也无法使用编辑器的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.将源Texture2DGraphics.Blit放入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()加载时失败.

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天全站免登陆