Make texture2D readable in runtime/script Unity3D [duplicate]
Asked Answered
M

1

3

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

Meraree answered 16/1, 2016 at 14:58 Comment(1)
Hi, do you have done this successfully? I have met with the exactly problem, And I found Texture needs to be marked as Read/Write to be able to GetRawTextureData in player trying the accepted answer :-(Maure
I
3

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

If answered 18/1, 2016 at 8:36 Comment(7)
How do you make sure markNonReadable is false? Or how do you set it to false?Meraree
markNonReadable is an optional parameter in LoadImage(). More here: docs.unity3d.com/ScriptReference/Texture2D.LoadImage.htmlIf
Hi, @Kimimaru, I am dealing with the same problem as the OP met, trying your solution of trying to use GetRawTextureData() to build a new Texture2D but meet with Texture needs to be marked as Read/Write to be able to GetRawTextureData in player and it seems I have to get a Read/Write able Texture to use the GetRawTextureData() method, which put me into the first place again, any advice? Or did I do it wrong? hope for your response, thanks in advance.Maure
@Maure Is the texture you're getting the data for in the project? If so you can do the steps I mentioned above and mark it Read/Write in the inspector.If
@Kimimaru unfortunately the image was loaded by a jar from the gallary,haven't accase the code of the jar,so can't change by inspector.Maure
This answer is wrong. You can't use LoadImage with GetRawTextureData. It has to be LoadRawTextureData because LoadImage is used for pngs and jpegs. See this for proper way to do this.Bias
Programmer, thanks for the details and link! The RenderTexture idea seems the best way to do this. @Maure please try Programmer's link.If

© 2022 - 2024 — McMap. All rights reserved.