Make texture2D readable in runtime/script Unity3D [duplicate]

妖精的绣舞 提交于 2019-12-22 17:36:19

问题


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?

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/34828372/make-texture2d-readable-in-runtime-script-unity3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!