如何通过脚本使Texture2D可读
我想让用户能够解码从图库加载的QR图像,我找到了一个插件来探索并加载图像作为texture2D,但要解码那个QR码,Texture2D必须是可读/可写的,我检查插件,对于Android,它正在使用jar进行探索和加载,在IOS平台中,它使用的是打包的库,因此我无法访问lib的代码,
我已经搜索了答案,最大的解决方案是在Unity检查器中更改纹理的导入设置,但由于这是由代码加载的纹理,因此没有可用的检查器设置,所以我的问题是:
有没有办法让这个加载的纹理可以通过代码读/写? 无需访问lib代码?
谢谢
这是可以通过此插件获取纹理的代码
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): 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
变量。
有两种方法可以做到这一点:
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来执行此操作,因为它不会引发任何错误。
- 列表框ItemTemplate选择器不选择模板
- 为什么字典键是不可变的?
- TextInputEditText:ArgumentNullException:MvxWeakEventSubscription中缺少源事件信息
- IRouteConstraint MVC4上的HttpContextBase.Session和HttpContext.Current.Session都为null
- 获取错误:“在SQL中将数据类型nvarchar转换为数字时出错”
- 从代码中关闭Silverlight中的Out of Browser应用程序
- 无法让Log4Net在我们的WCF应用程序中运行
- 创建新事件到iCloud Apple日历始终结果400错误请求
- 是否可以在Firefox或Chrome中查看post请求的数据?