我想让用户能够解码从图库加载的QR图像,我找到了一个插件来探索并加载图像作为texture2D,但是为了解码那个QR码,Texture2D必须是可读/可写的,我检查了插件,对于Android,它正在使用jar进行探索和加载,在IOS平台中它使用打包的库,因此我无法访问lib的代码,
我已经搜索了答案,最常见的解决方案是在Unity检查器中更改纹理的导入设置,但由于这是由代码加载的纹理,因此没有可用的检查器设置,所以我的问题是:
有没有办法让这个加载的纹理可以通过代码读/写?无需访问lib代码?
由于
以下是可以通过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));
}
正如你所看到的,我试过this answer但是没有运气。
以下是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
变量。
答案 0 :(得分:13)
有两种方法可以做到这一点:
1 。使用RenderTexture
(推荐):
使用RenderTexture
。将来源Texture2D
放入RenderTexture
Graphics.Blit
,然后使用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 来执行此操作,因为它不会引发任何错误。