Search Unity

[Released] Animated GIF Player

Discussion in 'Assets and Asset Store' started by Mareck, Mar 8, 2017.

  1. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi everyone,

    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).
    Link: https://www.assetstore.unity3d.com/en/#!/content/80956

    Updates:
    • V1.14.0
      • Bug fixes
      • 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
    • V1.02
      • Fixed playback on WebGL
    • V1.01
      • Bug fixes
    • V1.00
      • First release
     
    Last edited: Oct 31, 2018
    dyupa and thecotanak like this.
  2. thecotanak

    thecotanak

    Joined:
    Sep 2, 2012
    Posts:
    12
    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.
     
    Last edited: Mar 16, 2017
  3. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    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
    Code (csharp):
    1. var path = Path.Combine("file:///" + Application.streamingAssetsPath, FileName);
    To:
    Code (csharp):
    1. #if UNITY_ANDROID && !UNITY_EDITOR
    2.             var path = Path.Combine(Application.streamingAssetsPath, Uri.EscapeUriString(FileName));
    3. #else
    4.             var path = Path.Combine("file:///" + Application.streamingAssetsPath, Uri.EscapeUriString(FileName));
    5. #endif
    Line 450:
    Code (csharp):
    1. _nextFrameTime = Time.time + CurrentFrame.delay;
    To:
    Code (csharp):
    1. _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!
     
    Last edited: Mar 17, 2017
  4. thecotanak

    thecotanak

    Joined:
    Sep 2, 2012
    Posts:
    12
    Thanks for the quick update but sadly, still doesn't work on mobile. Tried replacing the code, but nope :/
     
  5. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    I have updated the above code to fix that. Tested on both Android and iOS as working
     
  6. thecotanak

    thecotanak

    Joined:
    Sep 2, 2012
    Posts:
    12
    Working now, thanks!
     
  7. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    A new update has been released which fixes playback on WebGL.
     
  8. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    A new update has been released:
    V1.1:
    • Changed namespace to "OldMoatGames". If you are updating from 1.0 change "using AnimatedGifPlayer;" to "using OldMoatGames;" in your code
    • Can load GIFs from web instead of just StreamingAssetsFolder. Only usable 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
     
  9. PabloAM

    PabloAM

    Joined:
    Dec 25, 2012
    Posts:
    35
    Hello, looks awesome your plugin :)

    But I´m having a problem playing in a RawImage.

    I have a RawImage inside a gameobject, if I try to activate the parent gameobject some Gifs works, and others doesn´t work...

    Attach Gifs working (60fps.gif) and gifs not working (Photoshop.gif).

    Best regards
     

    Attached Files:

  10. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi,

    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)?
     
  11. PabloAM

    PabloAM

    Joined:
    Dec 25, 2012
    Posts:
    35
    UPDATED: I found the problem. The problem was that I set:
    Code (csharp):
    1. Time.timeScale = 0;
    Do you think It would be a good idea add an option in the script in order to avoid the TimeScale when is playing Gif?

    Thanks!
    Best Regards.
     
    Last edited: May 18, 2017
  12. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Glad you found the cause :).

    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:


    Code (CSharp):
    1.  
    2. private void UpdateFrameTime() {
    3.    if (State != GifPlayerState.Playing) return; // Not playing
    4.    _secondsTillNextFrame -= Time.realtimeSinceStartup - _editorPreviousUpdateTime;
    5.    _editorPreviousUpdateTime = Time.realtimeSinceStartup;
    6. }
    7.  
    With this change timescale has no effect anymore on playback speed
     
    Last edited: May 18, 2017
  13. PabloAM

    PabloAM

    Joined:
    Dec 25, 2012
    Posts:
    35
    Thanks for reply Mareck :)

    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 :)

    Best regards
     
  14. Abolfazl2017

    Abolfazl2017

    Joined:
    May 5, 2017
    Posts:
    2
    Hi.
    Does it work properly with transparent gif?
    like this animated-dinosaur-image-0033.gif
    If i assign this gif to a plane can I see Objects in back of this plane(where the opacity is zero on gif)?
     
    Last edited: Jun 12, 2017
  15. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi,

    It sure does work with transparent Gifs.

    transparent-animated-gif-player.PNG

    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.
     
  16. Abolfazl2017

    Abolfazl2017

    Joined:
    May 5, 2017
    Posts:
    2
    I purchased it and works perfect . Sprite Example is exactly what I want.
    Thanks
     
    Last edited: Jun 15, 2017
  17. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Version 1.11 has been released.

    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.
     
  18. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Version 1.12 was just released. This update adds an option (code only) to also use persistentDataPath and temporaryCachePath instead of just streamingAssetsPath.
     
  19. dearamy

    dearamy

    Joined:
    Mar 17, 2015
    Posts:
    68
    Hi Mareck
    I just got the asset and every time I select a gif of my own I got an error.
    I was wondering is there anything wrong with my gif?

    ArgumentNullException: Argument cannot be null.
    Parameter name: buffer
    System.IO.MemoryStream..ctor (System.Byte[] buffer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/MemoryStream.cs:80)
    (wrapper remoting-invoke-with-check) System.IO.MemoryStream:.ctor (byte[])
    OldMoatGames.AnimatedGifPlayer+<Load>c__Iterator0.MoveNext () (at Assets/AnimatedGifPlayer/Scripts/AnimatedGifPlayer.cs:339)
    OldMoatGames.AnimatedGifPlayer.Init () (at Assets/AnimatedGifPlayer/Scripts/AnimatedGifPlayer.cs:242)
    AnimatedGifPlayerEditor.OnEnable () (at Assets/AnimatedGifPlayer/Editor/AnimatedGifPlayerEditor.cs:17)
    Fly.gif
     
    Last edited: Jul 22, 2017
  20. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Does this also happen when you select one of the included gifs? Also, what version of Unity are you using?
     
  21. dearamy

    dearamy

    Joined:
    Mar 17, 2015
    Posts:
    68
    Yes, and it seems to happen to all the optimized .gif files and I'm using Unity 2017.1 on mac os.
     
  22. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    I didn't have a chance to test the player with unity 2017 yet, so probably there were some breaking changes.

    I will take a look this and post a fix in this thread later today.

    Sorry for the inconvenience.
     
  23. SolusNZ

    SolusNZ

    Joined:
    Jan 6, 2014
    Posts:
    5
    Hi there,

    I'm trying to use this plugin in conjunction with the uLivewallpaper library, but I'm finding it difficult to load files from a device (Android) into the AnimatedGifPlayer object.

    I was excited to see in the changelog that URL's are supported, because I thought I might be able to point to an internal file url such as "file:///data/user/0/com.Solus.Obelisk/files/test.gif"

    In my Android device log, I see the following error:

    07-25 15:31:01.438 30529-30562/com.Solus.Obelisk W/Unity: File load error.
    java.io.FileNotFoundException: JAR entry assets/file:///data/user/0/com.Solus.Obelisk/files/test.gif not found in /data/app/com.Solus.Obelisk-2/base.apk


    How would I need to specify a URL in C#, would if be via the AnimatedGifPlayer.FileName property? Or is there another exposed property to load from a URL?

    Thanks,
    Mark
     
  24. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    2017 works fine under Windows on my machine. I don't have a Mac to this this under MacOs so it is a little tricky to solve this for me :).

    I think the path to the file is set wrong. Can you in AnimatedGifPlayer.cs add after (around line 322):

    Code (CSharp):
    1. path = System.IO.Path.Combine("file:///" + gifPath, Uri.EscapeUriString(FileName));
    Add:

    Code (CSharp):
    1. Debug.Log("Using path:" + path);
    It should debug the path to the console now when you start it. What path does it report?
     
  25. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi,

    I only added the ability to load from those preset folders that are build into Unity (StreamingAssetsPath, PersistentDataPath and TemporaryCachePath). I can add the option to completely set your own path in a future update.

    For now you can also edit AnimatedGifPlayer.cs to modify its behaviour to do just that. Just change around line 320:

    Code (CSharp):
    1. path = System.IO.Path.Combine(gifPath, Uri.EscapeUriString(FileName));
    to:

    Code (CSharp):
    1. path = FileName;
    And use AnimatedGifPlayer.FileName to set the correct path (ie file:///data/user/0/com.Solus.Obelisk/files/test.gif).

    Also, please make sure the path is correct and is set as you would normaly load a file when using WWW (https://docs.unity3d.com/ScriptReference/WWW.html)
     
  26. dearamy

    dearamy

    Joined:
    Mar 17, 2015
    Posts:
    68
    After selecting one of my own gifs the console reports the debug and an error and the gif doesn't show up:
    Screenshot 2017-07-26 12.28.09.png
    and then I selected one of the example gifs, the console reports the path with a suffix like this
    Screenshot 2017-07-26 12.28.26.png
     
    Last edited: Jul 26, 2017
  27. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    It seems the path is incorrect. it shows file:////Users.... instead of file:///Users.... Can you try without a leading slash?
     
    Last edited: Jul 26, 2017
  28. SolusNZ

    SolusNZ

    Joined:
    Jan 6, 2014
    Posts:
    5
    Hi Mareck,

    In relation to the Android Path issues,

    Your suggestion of overwriting the "path = System.IO.Path.Combine(gifPath, Uri.EscapeUriString(FileName));" statement has worked.

    The working file is "file:///data/user/0/com.Solus.Obelisk/files/test.gif"



    In Unity C#, I have noticed the following when using the following params:

    AnimatedGifPlayer.Path = GifPath.PersistentDataPath;
    AnimatedGifPlayer.FileName = "test.gif";

    This ends up with the following error:

    "java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.Solus.Obelisk/files/test.gif (No such file or directory)"

    Forcing the path to "file:///storage/emulated/0/Android/data/com.Solus.Obelisk/files/test.gif" generates the same error.
    Anyway, I don't really understand the difference between those directories, or why one works and the others doesn't.

    Thanks,
    Mark
     
  29. dearamy

    dearamy

    Joined:
    Mar 17, 2015
    Posts:
    68
    Yes! it worked
     
  30. mhaze618

    mhaze618

    Joined:
    Sep 16, 2015
    Posts:
    3
    Forgive me for being a noob but how can I restart the AnimatedGif before its over on a event of keypress? I can start it just fine call Play()
    I was using this for my old animator for restart
    Code (CSharp):
    1. AIScreen.GetComponent<Animator>().Play("PodestaAI", -1, 0);
    Having trouble figuring out how to Stop this AnimatedGIF playback and then maybe run a couroutine to start playing agiain. Like

    Code (CSharp):
    1. Yield for seconds xx
    2. GameObject.Find("PodestaAI").GetComponent<AnimatedGifPlayer>().Play();
    Or just maybe there is a easier way? Just need it to begin agian
     
  31. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Right now there isn't a method to reset the player to the start. I will add that in the next update. For now you can use the Init method again to reset playback to the start again.
     
  32. moodiey

    moodiey

    Joined:
    Jan 4, 2017
    Posts:
    16
    Hi Mareck. I just wanna report that i had the same error dearamy had and taking one slash out of the path on line 325 (AnimatedGifPlayer.cs) fixed the issue. Thanks!
     
  33. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Glad it is working for you now. This was also on Android?
     
  34. moodiey

    moodiey

    Joined:
    Jan 4, 2017
    Posts:
    16
    Nope! Unity 2017 on macOS
     
  35. Shukan

    Shukan

    Joined:
    Sep 22, 2017
    Posts:
    3
    Hello
    I have a problem with iOS. My gif-animation runs well with this player on Android, but why it doesn't work on iOS?
    The gif is downloding from my Unity project. I have tested on iphone and ipad.
     
  36. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    The player is tested on iOS and should work just fine. How are you using the player? Just with the component or from code?
     
  37. Shukan

    Shukan

    Joined:
    Sep 22, 2017
    Posts:
    3

    I used it with the component and I put the gif into the folder "StreamingAssets". In the script of Animated GIF player I do the "Select Gif" and choose it from the "StreamingAssets" folder.

    I have also tried to indicate the way to Gif from the code and transfer it to player. Likewise the CodeExample script.

    The build was done on Mac but in the Animated GIF Player the Gif is not displayed as it is on Windows.
     
  38. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    This is probably the same problem that was posted before. Can you try and remove on of the slashes in file:/// on line 325?
     
  39. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    A new update has been release:

    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
    I hope the problems with Mac Os are solved now. My old Mac broke so it is kinda difficult to to do proper testing right now :)
     
  40. genya127

    genya127

    Joined:
    Feb 7, 2017
    Posts:
    7
    Hello, @Mareck
    I have purchased Animated Gif Player today and it looks great
    But I have some problems with it
    My problem is how can I set Gif image dynamically using script?
    I want to change gif images using script
     
  41. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    @genya127

    Please take a look at the code example scene that is included.

    You can change the file it is using like this:

    Code (CSharp):
    1.  AnimatedGifPlayer.FileName = "AnimatedGIFPlayerExampe 3.gif";
    Then restart playback by calling the init method again:
    Code (CSharp):
    1. AnimatedGifPlayer.Init();
     
  42. warmanw

    warmanw

    Joined:
    Feb 2, 2015
    Posts:
    32
    Hi guys, before buying your plugin I have a question. If i put the effect on the object then export that objects prefab as assetbundle should I also need to download gif separately and assign it to already downloaded object or it will come within the assetbundle and no additional work will be needed?
     
  43. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    @warmanw

    To be honest I don't have much experience with assetbundles. But my understanding is that only Unity objects can be placed in them. Since a Gif isn't a Unity object you won't be able to store it there (That it is also the reason the Gifs have to be placed in the StreamingAssets folder in the first place)

    So probably you should download the Gifs separately and assign them to the object through code (there is an example included in the asset that shows you how to do that)
     
  44. warmanw

    warmanw

    Joined:
    Feb 2, 2015
    Posts:
    32
    Ok do you have other components like this where we can use sprite sheets we want to allow our players to buy Working TVs

    And if no is there a demo version of your plugin so I can test before buy
     
  45. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    I am afraid I don't have a solution that uses sprite sheets. The way the gif player was built is that it reads the frames from the gif itself and decodes and transfers them to the texture when needed, instead of recoding the gif into a texture atlas or sprite sheet ahead of time. This was done to save space, keep usage of the gif player as simple as possible and make sure that the player works for as many configurations and devices as possible.

    At the moment there is no demo version I can offer. However, you can always purchase the asset and get a refund later on if it turns out the asset is not suitable for your use case.
     
  46. warmanw

    warmanw

    Joined:
    Feb 2, 2015
    Posts:
    32
    Thanks for the answers, Please be kind to help me with more.
    Wont your plugin increase draw calls?
    Since our game is live now I cant change mechanics of download and I need to have one bundle for each item, I see unity can mark gif as assetbundle(but I assume it sees it as a texture) but I dont understand how to get the gif from assetbundle. If you know any info about please share with us.
     
  47. Shukan

    Shukan

    Joined:
    Sep 22, 2017
    Posts:
    3
    After we tryed the update everything runs well on ios.
    Thank you once again for the update!
     
  48. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    @warmanw
    Since each frame of the gif is uploaded to the same texture when a new frame is shown it is just a single drawcall per gif. (If you want to use multiple instances of the same gif at the same time I would recommend reusing the same texture that is created by the gif player to keep draw calls down).

    You are correct that the assetbundle just sees it as a texture that is converted from the gif. It also doesn't support animated gifs. One thing that comes to mind that you could try is storing the gif with an online image sharer and downloading it from there with the gif player. It has an option to use gifs from an http source.
     
  49. warmanw

    warmanw

    Joined:
    Feb 2, 2015
    Posts:
    32
    @Mareck hah So if the script has option to use links I can put gif to server and use link right?This would be nice I can use one bundle which will be downloaded by unity and while used script will start to download GIF right?
     
  50. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Yes you can use gifs from a http or https source with the animated gif player. Please note that this doesn't use any caching mechanism so each time you enable the gif it we reload from the url source.

    Best would be to write your own code to download and save the gif to the persistent datapath and set the gifplayer to use that datapath as a source.