问题
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