使Texture2D在运行时/脚本Unity3D中可读 [英] Make texture2D readable in runtime/script Unity3D

查看:506
本文介绍了使Texture2D在运行时/脚本Unity3D中可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插件,可以让我从Android手机库访问图片.这为我提供了Texture2D类型的纹理.然后,我想使用GetPixels函数对其进行编辑,但默认情况下未将其设置为可读.如何使纹理可读,以便可以在其上使用GetPixels?

I have a plugin that allows me to access pictures from an Android phones gallery. This gives me a texture of the Texture2D type. I then want to edit it using the GetPixels function, but it is not set to readable by default. How would I make the texture readable so I can use GetPixels on it?

基本上,我允许用户从手机中选择图片,然后进行裁剪.在以下示例图片中,图片将被红色矩形裁剪.如果我事先使纹理可读,则可以使用. http://puu.sh/mxR3h/dfa81719b2.jpg

Basically I am allowing the user to select a picture from the phone and then crop it. In the following example pic the picture would be cropped by the red rectangle. Which works if I make the texture readable beforehand. http://puu.sh/mxR3h/dfa81719b2.jpg

推荐答案

如果项目中有文件,则可以在检查器中选择纹理,将纹理类型设置为高级",然后设置读取和写入"启用"为true.

If you have the files in your project, you can select the texture in the inspector, set the texture type to "Advanced," then set "Read and write enabled" to true.

如果没有,您可以尝试在具有的纹理上使用GetRawTextureData(),然后创建具有相同宽度和高度的新纹理,并使用从旧纹理获得的数据在新纹理上调用LoadImage(),确保markNonReadable为false.然后,您应该能够在新纹理上执行所需的操作,并在用户裁剪图像时显示该纹理.

If not, you can try using GetRawTextureData() on the texture you have, then create a new texture with the same width and height and call LoadImage() on the new texture with the data you got from the old one, making sure markNonReadable is false. Then you should be able to do what you want on the new texture and display that while the user is cropping the image.

http://docs.unity3d.com/ScriptReference/Texture2D.GetRawTextureData.html

这篇关于使Texture2D在运行时/脚本Unity3D中可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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