Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Released] Animated GIF Player

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

  1. warmanw

    warmanw

    Joined:
    Feb 2, 2015
    Posts:
    32
    Thank you Mareck, You are great I will purchase it tomorrow
     
  2. dyupa

    dyupa

    Joined:
    Dec 21, 2016
    Posts:
    9
    Hi, Mareck!
    Can you please tell me, how should I modify AnimatedGifPlayer.cs to get it work with Projects, which have a special character in their name? For example, а "#" sign. The problem is, that when my project folder is containing this sign, GIF files can not load. And I am getting this message "Error loading gif" in console. But when I remove this "#" sign from the name of the folder, everything start working fine.
    Thank you!
     
  3. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    dyupa likes this.
  4. dyupa

    dyupa

    Joined:
    Dec 21, 2016
    Posts:
    9
    Thank you very much, Mareck! ;) I will try this tomorrow.
     
  5. dyupa

    dyupa

    Joined:
    Dec 21, 2016
    Posts:
    9
    Hi, Mareck!
    Sorry for late reply. I have searched today a lot about WWW class and tried to modify AnimatedGifPlayer.cs in different ways, but didn't get any success. I get "Error loading gif" every time. Unfortunately, I am not too strong enough in scripting right now to solve this problem. Can you please, Mareck, test it by yourself, if you have free time. If I have encountered this problem, I think, someone may encounter this problem too in the future. Maybe you will find a solution and add it to the next update.
    Thank you, Mareck! ;)
     
  6. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi,

    You could try replacing line 335 in AnimatedGifPlayer.cs:
    Code (CSharp):
    1. using (var www = new WWW(path)) {
    with:
    Code (CSharp):
    1. using (var www = new WWW(path.Replace("#", "%23"))) {
    This will encode the # correctly.
     
    dyupa likes this.
  7. dyupa

    dyupa

    Joined:
    Dec 21, 2016
    Posts:
    9
    Hi, Mareck!
    Many thanks for your help! ;) Everything works fine now. Great support and great asset from you. Thank you! :)
     
  8. pablouwall

    pablouwall

    Joined:
    Apr 20, 2015
    Posts:
    18
    i'm purchase this yesterday and doesnt work for Android too,im mac users ....
    work fine in desktop version but not on mobile need some help
    #if (UNITY_ANDROID && !UNITY_EDITOR) || (UNITY_WEBGL && !UNITY_EDITOR)
    path = System.IO.Path.Combine(gifPath, Uri.EscapeUriString(FileName));
    #else
    path = System.IO.Path.Combine("file://" + gifPath, Uri.EscapeUriString(FileName));
    #endif
    }
    is not the same with your first solution
    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
     
  9. pablouwall

    pablouwall

    Joined:
    Apr 20, 2015
    Posts:
    18
    Nothing?
     
  10. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    What version of Unity and the Gif Player are you using?
     
  11. UDN_165a1249-a0c9-4099-8ba0-53daf2789359

    UDN_165a1249-a0c9-4099-8ba0-53daf2789359

    Joined:
    Nov 1, 2016
    Posts:
    12
    i'm purchase this and doesnt work for Android, please help....
    unity 2017.3
    Gif Player 1.13v
     
  12. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    A new version has been uploaded to the asset store that fixes the Android problems. Once Unity has reviewed the submission it should be available for download.
     
  13. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    The new version (1.13.1) has been approved by the asset store and should now be available for download. This version should fix the problems with the latest Unity version and Android. Also, a memory leak was fixed that could occur when the same component was used repeatedly to load different gifs.
     
    dyupa likes this.
  14. pablouwall

    pablouwall

    Joined:
    Apr 20, 2015
    Posts:
    18
    New update make my android build ok
    Thanks you so much !
     
  15. vmigo900

    vmigo900

    Joined:
    Jan 6, 2018
    Posts:
    1
    Greetings, I have a question regarding your asset. Is it possible for this to work in the social game VRChat? I added a gif to a cube and then attached it to my avatar and when I tried to upload it, Unity said I needed to remove the client. Any suggestions?
     
  16. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi, I am not familiar with VRChat but this asset should work in all kind of builds since it is pure c#.

    Can you provide a screenshot of the exact error message?
     
  17. lauraaa

    lauraaa

    Joined:
    Dec 30, 2016
    Posts:
    16
    Hello, thank you for the great asset!

    I have some questions regarding playing large amount of GIF, like 100 different ones. For this case, I set all GifPlayer to cache & buffer all the frames. Loading threads work well, but there are overheads when playing them.

    Currently when update the texture, it's updating the Texture2D with byte array every frame --
    Code (csharp):
    1. GifTexture.LoadRawTextureData(CurrentFrame.Image);
    I wonder if it'd help if all the frames are created beforehand, stored in a list of Texture2D, and just swapping them to the target when update?

    Do you think it'd makes sense and maybe provide an options for it? Or if you have other suggestions?
    Would love to get some help on this. Thank you!
     
  18. Mareck

    Mareck

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

    Are you working on something for mobile or pc? On pc the impact of playing many different gifs shouldn't be too bad for smaller gifs. If you buffer the frames the player does all the required work at once, so if you see a spike at startup you could try disabling buffering.

    The reason I chose this method (that is: uploading each frame to a texture when needed instead of having all frames available as a texture at all times) is because I wanted to limit (texture) memory usage. I found that uploading the frame data to the texture is pretty fast anyway and texture usage can quickly become very large (since you lose any compression the gif might have had) when you make them hold all frames all the time. Besides, you have to upload each frame to a texture once anyway.

    There isn't really an easy fix since changing the way this is handled in this asset would be a very large undertaking. If you are using this many gifs I would suggest splitting them into sprite atlases before building. There should be some assets out there that can help you converting gifs to sprite atlases.
     
  19. yesbradd

    yesbradd

    Joined:
    Apr 4, 2018
    Posts:
    7
    Hi Mareck!

    Just started using the package and i'm trying to open a gif from the persistent data path. At the moment im taking it out of an asset bundle. The gifs are appearing the the persisentdata path. I dont know why it wont work as it works on IOS but not android.

    The player does play gifs that are in streaming assets on both android and ios.

    Code (CSharp):
    1.    
    2.     public void FillGifAnimator(AssetBundle bundle , AnimatedGifPlayer gifPlayer , string gifName)
    3.     {
    4.         TextAsset a = bundle.LoadAsset<TextAsset>(gifName + ".txt");
    5.  
    6.         string writePath = Path.Combine(Application.persistentDataPath, gifName + ".gif");
    7.         DebugConsole.Log("Writeing to: " + writePath);
    8.  
    9.         File.WriteAllBytes(writePath , a.bytes);
    10.  
    11.  
    12.         gifPlayer.Path = GifPath.PersistentDataPath;
    13.         gifPlayer.FileName = gifName + ".gif";
    14.         gifPlayer.Init();
    15.     }
    16.  
    Cheers
     
  20. Mareck

    Mareck

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

    Sorry about this. Unity is a pain with file locations. It is not at all consistent across devices or versions. Could you try replacing
    Code (CSharp):
    1. path = System.IO.Path.Combine(gifPath, FileName);
    around line 339 with
    Code (CSharp):
    1. path = System.IO.Path.Combine("file://" + gifPath, FileName);
    If that doesn't work, could you place a debug.log(path) just before line 343 to see what the path is it has build.
     
  21. Paiolus

    Paiolus

    Joined:
    May 3, 2017
    Posts:
    1
    Hi, I would like to ask if your script or plugin works with transparent gifs too (or gifs with some transparency). I know that in 3d mode it is something relatively useless, but in 2d it may be a current issue. That's, for example, my case, as I want to make a main character which has different skins that work by gifs with transparency.

    Thanks and sorry for the disturbance.
     
  22. yesbradd

    yesbradd

    Joined:
    Apr 4, 2018
    Posts:
    7
    Hi Mareck!

    Unfortunately that didn't work. I have a debug set 351 to show the WWW error and it spits out "Unknown Error".

    Pretty Stuck at the moment.

    Cheers
     
  23. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    @yesbradd can you debug what the path value is?
     
  24. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    @Paiolus Yes it works with transparent gifs. The sprite example that is included has transparency. Please keep in mind that gifs can't have semi-transparent pixels though.
     
  25. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    @Mareck in the case where i want to keep swapping/loading new gifs, I modified the source so i could Load on demand (instead of on startup), and it plays fine, then i use Pause to stop. The first time I want to switch, I call Load again, and I get no errors, but it does nothing when played. What can I do to cleanly Unload, and prep it for Loading again?

    Thanks!
     
  26. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    This should be pretty easy.

    First stop playback with:
    Code (CSharp):
    1. AnimatedGifPlayer.Pause();
    Then set a new source GIF file with:
    Code (CSharp):
    1. AnimatedGifPlayer.FileName = "AnimatedGIFPlayerExampe 2.gif";
    To let the player start with stuff like loading the first frame and preparing the target call:
    Code (CSharp):
    1. AnimatedGifPlayer.Init();
    What is probably going wrong is that this is an async operation. The reason for this is that loading the gif and decoding the header to, among others, determine the pixel size takes some time. So you shouldn't call Play(); immediately after init, but from the callback function you set with
    Code (CSharp):
    1. AnimatedGifPlayer.OnReady
     
    Last edited: May 21, 2018
  27. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    Thanks for this. I think I'm pretty much doing that (using OnReady etc), except I'm not calling Init, I'm calling Load that I made public directly. But I'll try this way specifically tonight, thanks again!
     
  28. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    Oh btw, I don't know if it changes anything, but I'm using images on the web as well.
     
  29. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    This shouldn’t make any difference. It just takes longer to load from the web.

    The Init() method does the actual loading from disk or http and the parsing of the GIF header so it needs to be called after switching GIFs.
    In hindsight the method names could have been clearer. But till version 2.0 I don’t really want to make any breaking changes. Sorry for the confusion this has brought.
     
  30. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    No worries! Thanks man! I'll let you know how it goes.
     
  31. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    Worked like a charm! Thanks!
     
  32. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    You don't see any instances where there could be a memory leak produced doing this do you? I'm using this in a webgl app, and it's running out of memory after a while now.

    Thanks!
     
  33. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Are you using the newest version? I fixed a memory leak not too long ago.
     
  34. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    It should be, I just bought it last week. I'll keep you posted if it persists.
     
  35. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    I am working on it. I found a possible memory leak when you use threaded decoding and switch GIFs before they are done processing but since you use webgl that can't be the cause since the threading is not available there. I will keep looking and hope I will have something later today.

    edit:

    I didn't find a memory leak after all. I did fix a possible crash though. The new version with the fix has been submitted to the asset store for approval. It should be available in the next few days.
     
    Last edited: May 31, 2018
  36. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    Thanks!
     
  37. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    Hello guys. I bought this great asset today but I am facing some problems.
    - When I set CacheFrames and BufferAllFrames to TRUE, my app crashes. I used a log and found that _cachedFrames keeps increasing its size. Shouldn't the cache just cache all the frames and use them from the list?
    - You say in the comments that we should Reload the GIF for this to have effect. What do you mean? How we reload?

    Thank you in advance guys!!
     
  38. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Hi!

    _CachedFrames increasing in size would certainly be a bug. I will check what is happening and fix it asap.

    Reloading is done by calling the Init() method again. In hindsight, the naming of some methods isn't too descriptive in some cases. Sorry about that :)
     
  39. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    Ok, thank you for the information. So, I do call Init() after first init(), I reset CacheFrames and BufferAllFrames back to false but nothing happens. This is what I do after the first init() :
    Code (CSharp):
    1. Player.CacheFrames = false;
    2. Player.BufferAllFrames = false;
    3. Player.Init();
    But I guess, I should not call Init() again because I changed variables before first init(). So, it's a small buffer bug that instead of using the cached textures, it buffers and caches infinite.
     
  40. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    I have found and fixed a crash with the threaded decoder. I will upload the fix to the asset store and it should be available within a day or two. If you want you can apply the fix yourself. In AnimatedGifPlayer.gif at the end of the file insert:
    Code (CSharp):
    1. _decodeThread = null;
    directly after the line with:
    Code (CSharp):
    1. _threadIsCanceled = true;
    Next remove the following line after that:
    Code (CSharp):
    1. _wh.Close();
     
    Last edited: Jun 14, 2018
  41. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    Thank you again for your support and I am sorry for disturbing you. Did what you said but list keeps increasing it's size. I used
    Code (CSharp):
    1. Debug.LogError("SIZE : " + Player._cachedFrames.Count);
    and count goes infinite until device gets out of memory. Thank you in advance.
     
  42. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    That is very strange. Are you using the Init() method just once or repeatedly?
     
  43. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    Only once. After that I just use Play() and Pause() methods. Are you getting a standard size for the array if you its Count at the console? If yes, it must be my fault then :)
     
  44. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    I don't see this happening in the code example that is included. Could you give your code or try to change the example so that this happens?
     
  45. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    Hello friend.

    Sure. I initialize the GIF player by using these lines of code :
    Code (CSharp):
    1. Player.Loop = true;
    2. Player.CacheFrames = true;
    3. Player.BufferAllFrames = true;
    4. Player.OverrideTimeScale = true;
    5. Player.TimeScale = Speed;
    6. Player.AutoPlay = false;
    7. Player.UseThreadedDecoder = true;
    8. Player.FileName = (!url.Contains(".gif")?(url + ".gif"):url);
    9. Player.Path = GifPath.StreamingAssetsPath;
    10. Player.Init();
    Then on the Update() method and while the GIF in playing, I use this line of code :
    Code (CSharp):
    1. Debug.LogError("SIZE : " + Player._cachedFrames.Count);
    The console prints the List count and the count goes to infinite. Am I doing this wrong?
     
  46. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    I think I made it work. Setting Player.UseThreadedDecoder = false stops increasing the Array Size and cache successfully the GIF frames. ;)
     
  47. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    Yeah, it was some bug related to changing the caching and buffering settings when using threads. Setting "cache frames" and "buffer all frames" to true in the gif component also works btw if you want to use a thread to decode the gifs.

    I will upload the fix immediately to the asset store and it should be available within two days.

    Thank you very much for the bug report and helping me fix this! (let me know if you need the fix right away and I'll send the updated file so you don't need to wait for the asset store to approve the update.)
     
  48. gvergidis

    gvergidis

    Joined:
    Apr 5, 2018
    Posts:
    11
    Dear friend. My gif is small so I dont need the fix right away. It was my pleasure helping you!

    Thank you for your support and interest. See you around friend :)
     
  49. RenegadeNinjaNL

    RenegadeNinjaNL

    Joined:
    May 23, 2013
    Posts:
    22
    Hey,

    During a match I capture and create 1-3 GIFs. At the end of the match I try to display all the GIFs simultaneously. However, every time I try to do this I keep receiving this error:
    Code (CSharp):
    1. File load error.
    2. Couldn't read a file:// file
    3. Path:file%3a%2f%2fF%3a%2fDocuments%2fUnity+Projects%2fMad+World%2fAssets%5cUser%5cGIFs%5cGIF+035+-+Akuto_+Mad+World+-+Build+94.gif
    4. UnityEngine.Debug:LogWarning(Object)
    5. OldMoatGames.<Load>c__Iterator0:MoveNext() (at Assets/AnimatedGifPlayer/Scripts/AnimatedGifPlayer.cs:358)
    6. UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
    7.  
    Could you please help me on how I can resolve this issue? I tried only playing 1 GIF at the end of the match, but that doesn't seem to work either.
     
  50. Mareck

    Mareck

    Joined:
    Aug 13, 2014
    Posts:
    77
    It looks like the player can't find the gif at the file location. What is the full file path you are using? The path shouldn't be url encoded. Also: Is this on windows or mac and what Unity version? Also, are you using the latest version of the gif player?
     
    Last edited: Jun 21, 2018