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

Waltari
  • 1,052
  • 1
  • 30
  • 64
  • 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 :-( – armnotstrong Jun 23 '17 at 13:52

1 Answers1

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

  • How do you make sure markNonReadable is false? Or how do you set it to false? – Waltari Jan 18 '16 at 09:15
  • 1
    markNonReadable is an optional parameter in LoadImage(). More here: http://docs.unity3d.com/ScriptReference/Texture2D.LoadImage.html –  Jan 19 '16 at 09:10
  • 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. – armnotstrong Jun 23 '17 at 13:55
  • @armnotstrong 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. –  Jun 23 '17 at 22:09
  • @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. – armnotstrong Jun 24 '17 at 06:23
  • 1
    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](https://stackoverflow.com/a/44734346/3785314) for proper way to do this. – Programmer Jun 24 '17 at 10:55
  • Programmer, thanks for the details and link! The RenderTexture idea seems the best way to do this. @armnotstrong please try Programmer's link. –  Jun 25 '17 at 01:09