I’m downloading a GIF image (no choice, it’s the only captcha image format that Player.IO supports), and since Unity doesn’t support GIFs as GUI textures (correct me if I’m wrong) as a hack I’m trying to convert it from WWW.bytes to Jpeg or anything Unity does recognize with the System.Drawing.Image class whose dll I have compiling OK in Unity. My app is on the web player so it all needs to be done in memory.
Here’s my humble attempt, slapping together streams and byte arrays, the underlying nature of which I’m still new to. It ends up yielding a null reference exception:
Texture2D guiTexture;
WWW www;
//Download it here. www.texture and www.textureNonReadable are both the 8x8 question mark, but www.bytes seems to be filled with the correct 4 to 6K of data.
Image img=Image.FromStream(new MemoryStream(www.bytes));
Debug.Log("Image type: "+img.GetType());//Gives: "System.Drawing.Bitmap". Does that mean it doesn't recognize it as a GIF?
byte[] newbytes=new byte[100000];//Not sure how much it'll need; make it huge just in case.
Stream stream=new MemoryStream(newbytes,true);//It takes a stream so I'm creating one as a middle man
img.Save(stream,ImageFormat.Jpeg);//Tried also Png and Bmp.
stream.Write(newbytes,0,10000);//Not sure how many bytes to make it, since I'm not sure how to tell from Image.Save how many bytes the new converted image actually is.
myTexture.LoadImage(newbytes);//gives a "NullReferenceException [...] c__Iterator1.MoveNext ()"
The solution I can think of is send the GIF data from Player.IO to a php file that you have hosted and that php file converts the image and sends you back. well, this might be a over load if you have to do the conversion often in game.
There are lot of ways to convert image to png in php. There are external libraries that does that as well.
I think system.drawing does not work on unity. I think it is incompatible library with dx/opengl. However if you really want to use it, you can look on here
By the way, there is a good reason of not supporting gif. Gif works very differently. It is a disgusting format for moving photos, you barely have control for fps etc.However, if you like to work with animated textures/photos, you should use movie textures(pro only)/sprites(pro-free both). movie textures will be performance costly heavily. sprite is all the way to go!
This is how I do it,
1.I have the gui texture animation as a movie file(avi, mp4 or whatever etc)
2.I convert it into sequence of images
3.I convert them as a sprite sheet
4.I have a depth camera dedicated for gui rendering
5.On the camera, I convert gui sprite planes into viewport space.
6.Render and Done!
However, that workflow was when unity did not have 2D tools. I am figuring a smarter way of doing gui with 2D tools.