I am storing a PNG in a textfile. I convert it to a PNG using:
myTexture.EncodeToPNG()
and then save. I then load the bytes with:
Texture2D texture = new Texture2D(100, 100);
texture.LoadRawTextureData(myPNGBytes);
This throws the following error:
UnityException: LoadRawTextureData: not enough data provided (will result in overread).
I know that the PNG is stored correctly because when I use an online base64 converter the image is displayed. I’ve tried loading with several different formats, but all throw the same exception. What am I doing wrong?
EDIT: when I check the format of the Texture2D before saving, it says RGB24. I get the same error when I instantiate the texture with this format and then load.
EDIT: Even when I skip the saving step and just do:
byte[] bytes = result.Texture.EncodeToPNG();
Texture2D texture = new Texture2D(100, 100);
texture.LoadRawTextureData(bytes);
I get the same exception. So my question is what is the inverse of Texture2D.EncodeToPNG()