UnityWebRequest.GetTexture and EncodeToPNG
Hi friends!
I need to ask you about this error with the UnityWebRequest.GetTexture function.
I have a code with the WWW class to get image from Azure, then I create sprites from this images and use them in the game. And before leave the scene, I need to save some of this sprites with EncodeToPng. All my code with the WWW class works. It´s something like this:
DownloadTexture(){
WWW www = new WWW (url);
yield return www;
Texture2D tex = new Texture2D (1, 1);
www.LoadImageIntoTexture(tex);
Sprite image = Sprite.Create (tex, new Rect (0, 0, tex.width, tex.height), new Vector2 (0.5f, 0.5f));
someGameObject.sprite = image;
}
SaveSprite()
{
byte []bytes = someGameObject.sprite.texture.EncodeToPNG ();
}
I changed some variables and some stuff for simplify the case. With this code works perfect, but it´s a big data manage scene, so I tried UnityWebRequest for better performance. With the new WebRequest the performance is much better so I want to preserve this method, but When I try to save my sprites I have the error "Texture2D is not Readable" And don´t know how to change it. My new code is just a WWW to WebRequest change:
DownloadTexture(){
UnityWebRequest www = UnityWebRequest.GetTexture(url);
yield return www.Send();
tex = new Texture2D (1, 1);
tex = ((DownloadHandlerTexture)www.downloadHandler).texture;
Sprite image = Sprite.Create (tex, new Rect (0, 0, tex.width, tex.height), new Vector2 (0.5f, 0.5f));
someGameObject.sprite = image;
}
SaveSprite()
{
byte []bytes = someGameObject.sprite.texture.EncodeToPNG ();
}
Now I get my sprites I can play with them but can´t save them in local.
Does Anyone know how to save this in some other way or make the texture readable or something?
Thanks for your help! Have a good day!
Gabriel
Answer by wind-tsai · Jul 14, 2016 at 04:40 AM
Unity document say: public static Experimental.Networking.UnityWebRequest GetTexture(string uri, bool nonReadable);
the second parameter say that If true, the texture's raw data will not be accessible to script. This can conserve memory. Default: false.
But, after i tried to set it to be TRUE, i can read the raw data successfully.
Maybe the document make some mistake.
You're awesome! This worked for me, couldn't find the solution anywhere else.
Well spotted, this drove me crazy for hours! Not exactly intuitive that nonReadable
should in fact be read as readable
;-)
Your answer
Follow this Question
Related Questions
Change Texture2D readable flag at runtime (or at least a workaround to do that) 0 Answers
Unity gives me Not Readable error on texture, but i have it set! 0 Answers
Unity EncodeToPNG saving at 512x512, can't change dimension 0 Answers
Problem to get texture2D in IEnumerator with UnityWebRequest 0 Answers
How to make a script wait for a UnityWebRequest to finish for WebGL without a coroutine? 1 Answer