- Home /
Can I get the raw bytes of a Texture2D?
I would like to be able to make an exact copy of a Texture2D in any TextureFormat including mipmaps, etc.
The most promising API for creating the texture is http://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html as this API gives you the exact texture you put in. But, there doesn't seem to be an analogous GetRawTextureData to get the data from.
Is it possible to get the byte[] from an existing Texture2D compatible with LoadRawTextureData?
The most promising APIs look like starting with GetNativeTextureID and GetNativeTexturePtr... but this looks like a deep hole to start digging.
If by "until recently" you mean "until today", then yeah. ;)
Ha, how serendipitous! Guess it's your lucky day, $$anonymous$$idnight!
@zach.r.d Should you add this as an answer I will gladly accept it
I converted Zach's comment to an answer. The remaining comments here make more sense under that answer, but I don't see a way to move them.
Answer by zach-r-d · Jun 09, 2015 at 09:58 PM
Check out Texture2D.GetRawTextureData, it seems like the example provided is doing exactly what you describe. What version of Unity are you running? Perhaps it wasn't added until recently.
Answer by Esildor · Jun 10, 2015 at 08:52 AM
Try Texture2D.EncodeToPNG();
This function returns the texture as a byte array
Here's the documentation:
This will not do mipmaps (as far as I know), nor does it support all the TextureFormats.
Your answer
Follow this Question
Related Questions
Serializing AudioClip or Texture2D correction 1 Answer
Can you save a texture to android with orientation from unity? 1 Answer
How to properly load textures? 2 Answers
How to get mipChain and linear values from existing Texture2D 0 Answers
Is there a serialization class similar to JsonUtility but for Byte Serialization. 1 Answer