【发布时间】:2017-11-27 18:23:17
【问题描述】:
我想让用户能够解码从画廊加载的 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 变量。
【问题讨论】:
-
读取错误在哪里?贴出这行代码。
-
@Programmer,很高兴再次见到你,刚刚编辑发布日志
-
再次阅读您的问题并决定删除旧问题。我错过了一条重要的线路,即您使用的
Texture2D.LoadImage函数。如果现在一切正常,请告诉我。