Animated GIFS

Does Unity support Animated GIFS?

If yes, is there anything special I need to do in order for the animated GIFS to actually function properly on game objects or GUI textures?

I’d like to use them as textures on objects such as water, lava, etc =)

Any information is greatly appreciated! Thank you!

–Velk

2 Likes

Nope, but you can save the individual frames as separate textures, and cycle through an array of those textures. In some cases (if you don’t need tiling, for example), you can save all the frames into one texture and animate the texture offset. I think there’s a script on the wiki for that.

–Eric

1 Like

Thanks Eric5h5,

Thanks for the information…do you know how I can tell Unity to cycle through a series of textures (specifically, animated GIFS?) I’d like to use Animated GIFS as textures in my game.

So far, I’ve tried attaching the following script to my gameobject with the Animated GIF as it’s material/texture:

// this line of code will make the Movie Texture begin playing
renderer.material.mainTexture.Play();

Any ideas? I’m still learning how to script – so any information is greatly appreciated. Thank you!

As I said, you can’t use animated GIFs, but you can save the individual frames as separate textures, and cycle through an array of those textures:

var frames : Texture[];
var framesPerSecond = 10;

function Update() {
	var index : int = (Time.time * framesPerSecond) % frames.Length;
	renderer.material.mainTexture = frames[index];
}

–Eric

6 Likes

Thank you so much for the amazing script Eric5h5!

It works really well and solved my question(s) =)

Now that I’ve experimented with some of the possibilities of animated GIFS in Unity…I was wondering if it would also be possible to import a movie (.mov, .mpeg, etc) and have Unity simply play the movie at 10 frames per second.

It is much easier for me to set what frames play per second compared to manually importing each image and linking them all to the textures within the script you provided me – thanks again for the script .

Does that make sense?

Any information is helpful =) Thanks!

–Velk :smile:

Yes; see “MovieTexture” in the docs for that. However, that only works with Pro. Also, it’s intended for actual movies rather than animated textures like water, etc.–there’s overhead for decompressing movies on the fly that you wouldn’t want with continuously animated textures, not to mention that you can’t use alpha channels in movies. I know having separate frames isn’t the most convenient thing ever, but it’s unlikely you have too many frames for animated textures due to memory concerns.

–Eric

Thanks for the information/quick response =)

I’ll stick with the script you posted – however, I have been trying to figure out a way to have a variable on the script that I can adjust from the Inspector.

This would allow me the ability set a specific frame that I’d like the “Animated GIF” to stop on.

Here is what I’ve tried so far:

WrapMode.ClampForever

What should I add to the script shown BELOW in order for it to allow my GIF animation to stop on a desired frame, preferably a variable I can tweak from the Inspector…

var frames : Texture[];
var framesPerSecond = 10;

function Update() {
   var index : int = (Time.time * framesPerSecond) % frames.Length;
   renderer.material.mainTexture = frames[index];
}

Once again, thank you for your help – any additional information is always appreciated =)

–Velk

That’s for animations on 3D objects. To stop on a desired frame, you could make a variable that’s exposed to the inspector, and in the Update function, compare that to the “index” variable, and only have the rest of the code run if “index” is less than or equal to that value.

–Eric

1 Like

There has to be a way to stop the script from cycling through the image sequence repeatedly. I’ve tried all kinds of additional code…nothing seems to stop the infamous GIF sequence from playing in Unity.

Any ideas? I’m an Arts guy more than a programmer so a line of code or a website link where I can find the information and research it would be much appreciated =) Thanks again!

–Velk

But this doesn’t work for GUITextures as the poster originally requested. How does one animate **GUITextures (**no meshes)

Instead of using renderer.material.mainTexture, you can assign the GUITexture’s texture property. Otherwise, the code is the same.

1 Like

This is C#
Start () …
public Texture2D [ ]frames;

OnGUI ()… (instead of Update)
int index = (int) (Time.time * framesPerSecond) % frames.Length;

Why are you resurrecting a thread from over a year ago with code that is horribly incorrect?

Try this goods.
ZNE GIF.
You can use GIF animated in Unity3D.

Using ZNE GIF as a Menu Background
SOLVED: Turns out my GIF is 1024 x 768 x 1 byte x 300 frames.
But ZNE expands it to 1024x768x4x300 thus 943 MB. Oops.
Changed the type to a GIF Movie and now memory use is fine.
(Not set to Load All Frames at first)


I got ZNE GIF and it crashes. I share a GUITexture, and a single Zne Gif Animation (Script)so I call update in each of my menu objects. Sitting in a menu doing nothing else uses up memory vey fast. 3.5 GB ram in about 10 seconds:!:

I narrowed it down to the line

 z.Update_Frame();

Notes:
(Also tried GifComponent_Update() and it has the same problem)
(The gif is 3.6 MB on disk, a full screen background)
(If I set to Load all frames at first, it burns up 2.3 GB of memory, then is stable)
(Loading in a image viewer only takes 31 MB for the whole viewer and image)
(My app is about 800MB after start up, in menus, not in-game yet, if I delete the ZNE Gif component.)
(Starting ZNE GIF alone takes about 1.7 GB, even if Zne Gif Animation is disabled, and the gif file removed)
Any ideas why?

Called from Update in all menu components: (Except the one menu that ‘owns’ the zneGifAnimation)

     public void _updateMenuBackgroundAnimation()
    {
        Note: (This has been changed to be zneGifMovie instead)
        zneGifAnimation z = MainMenu.thisInstance.GetComponent<zneGifAnimation>();
        if(z != null)
        {
            z.Update_Frame();
        }
    }

Then to draw it as a menu background:

	public void fillMenuBackground()
	{
		Texture t = mainMenuBackground;
		if(MainMenu.thisInstance.guiTexture != null)
		{
			t = MainMenu.thisInstance.guiTexture.texture;
		}
		GUI.DrawTexture( new Rect(0,0,Screen.width, Screen.height), t, ScaleMode.StretchToFill);
	}

You could also try a script like the following, which makes use of Mono’s System.Drawing library: http://wiki.unity3d.com/index.php/AnimatedGifDrawer

1 Like

or just think outside the box, and use tools unity provides in fun ways. I like to avoid any heavy scripting. I was working on replicating the Deadspace style of GUI for doors, ect. First i made my GUI images for the layers and made them 75% occ in gimp, and made the rest Trans on alpha. In Unity I just put them on very thin cubes. Layering the ultra thin cubes(with image textures, set to trans/Diffuse). then here is the lol part, just make animations in the animator and let them loop forever of them spinning. Of course a little java to make them spin, appear on collision, ect would be very easy as well. In the end the player sees your animated “texture” the way u want them to see. also works great for cheat gui, attach the planes to the camera, bam poor mans gui. Always find a way to cheat the system.

Hi there! The wiki tuto you mentioned worked fine, but how is that to put the gif on a model?

You can try converting the gif to an mp4 or other video format and then set that as a VideoTexture. It would still need a script to play, but it would be significantly simpler. Here is the VideoTexture tutorial:

As for gif to mp4 tools, there are plenty online converters out there.

Hi everybody,
I’ve found a really nice solution called UniGif. You can find the repository here:

Tested on Android, iOS, MacOS, Windows and Editor. May need some changes if load from StreamingAssets (Android).

Cheers,
Jaime