- Home /
Getting red question mark for texture animated jpg
Hi!
I'm quite new in Unity.. hope this question will make sense..
I'm getting from server a jpg format url which should be presented as texture. It works well for regular jpg but once the jpg is animated I'm getting a red question mark.
This is the code I'm using:
public RawImage image;
public static IEnumerator GetTexture(string url, object o, Action<Texture, object> callbackSucsses, Action<string> callbackFailed)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
callbackFailed("");
}
else
{
Texture myTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;
callbackSucsses(myTexture, o);
}
}
// callback sucsses
private void setUI(Texture texture, object index)
{
int i = (int)index;
image.texture = texture;
}
Function setUI is called even when the texture is presented as a question mark.
Example for animated jpg: animated_jpg_example
What can I do in order to get the animated jpg right? I dont even need the animation, first or middle frame of it will be enough.
Thank you very much, will appreciate any answer / direction :)
Answer by Bunny83 · Sep 15, 2020 at 06:12 AM
The JPEG format doesn't support any kind of animations. The image you've linked is actually a GIF image which Unity can not load at all at runtime. Also "textures" do not support any kind of animations either. Textures are just still images in graphics card memory. A similar question has been asked on StackOverflow.
So there are several options to solve your issue. The best solution is to actually open your gif image in a proper image editor and save the image / frame you want as an actual jpg. Renaming the file to "jpg" does not magically turn it into a jpg.
If you really need to load THAT exact file at runtime, you need a gif image loader. There are several larger libraries which you can use in Unity to load all sorts of image formats including GIF files. However most of them are not free. I worked on a GIF loader some years ago and it's actually working with many common gif variants.. However it's still in a "work in progress" state and not "that" user friendly. However it's not that hard to use either. The steps are essentially:
Create a GIFLoader instance
use one of the Load methods to load a GIF file either from file or from a stream.
The "imageData" list of the generated GIFImage contains the actual frames. You can call "DrawTo" on a frame and pass in a Color32[] array. The method will write the frame data to that array.
Once the frame has been written you can create a Unity Texture2d and use SetPixels32 with that color array.
Don't forget to call Apply on the texture. That's it.
In order to actually "play" the animation you have to draw the next frame on top of the same color array.
A lot gif animations use delta compression so an animation frame might not contain the whole image but only those pixels which have been changed. So you often can not randomly pick a frame from the list and draw it. The frame might need the information from the previous frame(s).