I have just released Animated GIF Player which allows you, as you might guess, to play animated GIFs in Unity. Usage is very simple. Just add the Animated GIF Player component to an object and select a GIF that was copied to your StreamingAssets folder. Example usage:
Key features:
Easy to use with no scripting required
GIFs are decoded in a separate thread for increased performance and can be buffered and/or cached if needed
Pure C# solution, no installation of extra plugins or editing to get everything to work on mobile
Includes commented source code
Great for short videoclips as a replacement for MovieTexture on mobile. For example short tutorial movies or in-game television screens. Just convert your movie clip to an animated GIF using one of the many free online tools available.
Works on all platforms. Should you encounter any problem despite this, please let us know and we will fix it right away).
Added compatibility mode to allow playback of a wider range of GIFs. This includes GIFs that use the previously unsupported (but rarely used) ârestore to previousâ disposal method
V1.13.5
Fixed looping when using the threaded decoder and caching frames
V1.13.4
Fixed a potential memory leak when changing caching settings during run time
V1.13.3
Fixed crash when reloading a GIF when using the threaded decoder
V1.13.2
Fixed crash when switching GIF images if the first image wasnât done loading
V1.13.1
Fixed Android builds on Unity 2017.2 and higher
Fixed tmp texture not being destroyed when loading a new gif with the same Animated Gif Player component
V1.13
Fixed issue with Gif files not loading when using the Unity editor on Mac Os
Fixed bug where in some cases the first frame of the Gif was incorrect
Sprites are no longer resized after starting Gif playback on them
Gif target sprites are created as full rect instead of tight mesh
Gif target textures are no longer created twice. This should improve startup performance somewhat for large Gifs
V1.12
Gifs can now be loaded from: Application.streamingAssetsPath, Application.persistentDataPath, Application.temporaryCachePath as well as http
V1.11
Added speed controls which allows changing playback speed independent of Time.timeScale
V1.1
Changed namespace to âOldMoatGamesâ. If you are updating change âusing AnimatedGifPlayer;â to âusing OldMoatGames;â in your code
Can load GIFs from web instead of just StreamingAssetsFolder. This feature can only be used from code. See the code example scene for more info
Fixed the duration of the visibility of the first frame when resuming playback with the Play() method
Added OnLoadError event
Added Width and Height vars that return the size of the GIF once loaded
Hello,
Instant purchase. Working great on editor, but when I try building for Android, it gives me an error:
Assets/AnimatedGifPlayer/Scripts/AnimatedGifPlayer.cs(450,55): error CS1061: Type AnimatedGifPlayer.GifDecoder.GifFrame' does not contain a definition for delayâ and no extension method delay' of type AnimatedGifPlayer.GifDecoder.GifFrameâ could be found. Are you missing an assembly reference?
EDIT: Found the problem. Maybe youâve made a case mistake (but Iâm not sure). Replaced â.delayâ with â.Delayâ and seems like itâs fixedâŚ
EDIT2: Doesnât work on Android Didnât test iOS yet.
I have uploaded a new version (1.01) to the asset store that fixes the bugs you are experiencing. The package needs to be approved first though. Since this can take some time and you will probably want to be able to use the gif player, these are the changes that you can make to file AnimatedGifPlayer.cs in order to fix the bugs yourself:
In AnimatedGifPlayer.cs:
Line 254:
Change
var path = Path.Combine("file:///" + Application.streamingAssetsPath, FileName);
To:
#if UNITY_ANDROID && !UNITY_EDITOR
var path = Path.Combine(Application.streamingAssetsPath, Uri.EscapeUriString(FileName));
#else
var path = Path.Combine("file:///" + Application.streamingAssetsPath, Uri.EscapeUriString(FileName));
#endif
Line 450:
_nextFrameTime = Time.time + CurrentFrame.delay;
To:
_nextFrameTime = Time.time + CurrentFrame.Delay;
The bugs were caused by some last minute changes I made before submitting the first release and forgetting that spaces in filenames are handled differently on iOS and Android than on the Windows editor.
Sorry for the inconvenience. If you run into any more problems or have any questions or requests please let me know!
Both GIFs work if I use them in the ui example that is included with the asset (This is in the editor on windows). Could you be more specific about when they donât work (are you playing the gif from code or are you just using the inspector for example)?
You mean an option that allows playback to continue when timescale is set to 0?
The GIF player uses Time.deltaTime to determine when a new frame should be played. This delta time takes the time timescale in consideration. So with lower timescale playback is slower.
I will add an extra option in the next update. For now you can replace the function called UpdateFrameTime around line 491 to 509 in the script AnimatedGifPlayer.cs with the following code:
private void UpdateFrameTime() {
if (State != GifPlayerState.Playing) return; // Not playing
_secondsTillNextFrame -= Time.realtimeSinceStartup - _editorPreviousUpdateTime;
_editorPreviousUpdateTime = Time.realtimeSinceStartup;
}
With this change timescale has no effect anymore on playback speed
Ok, I have fixed avoiding the set timescale to 0 when I show tutorial gif.
But yes , it will be an awesome new feature the option to activate or desactivate timescale effect
Hi.
Does it work properly with transparent gif?
like this
If i assign this gif to a plane can I see Objects in back of this plane(where the opacity is zero on gif)?
To get this I used the MeshRenderer example that is included. Just create a new material and set the rendering mode of that material to transparent. Then go to the GifTexture object, select the mesh renderer and replace the default-material with this new material.
In this version speed controls have been added that allow changing the playback speed of the Gif. The speed is relative to the normal speed as encoded in the Gif. So, for example, setting it to 2.0 will double the playback speed.
Also note that the gif player will not try to catch up if the fps of the game are lower than the playback speed of the gif. In other words: even when the playback speed is very high, the player will not go faster than the fps of the game. No frames are ever skipped.
Version 1.12 was just released. This update adds an option (code only) to also use persistentDataPath and temporaryCachePath instead of just streamingAssetsPath.