Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animated gif's

Discussion in 'Scripting' started by tawdry, Sep 29, 2014.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi folks. I'm mostly clueless when it comes to programming outside of the basics. I am using unity free and wanted to try gif's for water effects but they do not play I found this script online but can't get it to work I think I must be missing some steps maybe. Here's the script and after i will say what i have tried .

    Code (CSharp):
    1. C# - AnimatedGifDrawer.cs
    2. using System.Collections.Generic;
    3. using System.Drawing;
    4. using System.Drawing.Imaging;
    5. using UnityEngine;
    6.  
    7. public class AnimatedGifDrawer : MonoBehaviour
    8. {
    9. public string loadingGifPath;
    10. public float speed = 1;
    11. public Vector2 drawPosition;
    12.  
    13. List<Texture2D> gifFrames = new List<Texture2D>();
    14. void Awake()
    15. {
    16. var gifImage = Image.FromFile(loadingGifPath);
    17. var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
    18. int frameCount = gifImage.GetFrameCount(dimension);
    19. for (int i = 0; i < frameCount; i++)
    20. {
    21. gifImage.SelectActiveFrame(dimension, i);
    22. var frame = new Bitmap(gifImage.Width, gifImage.Height);
    23. System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
    24. var frameTexture = new Texture2D(frame.Width, frame.Height);
    25. for (int x = 0; x < frame.Width; x++)
    26. for (int y = 0; y < frame.Height; y++)
    27. {
    28. System.Drawing.Color sourceColor = frame.GetPixel(x, y);
    29. frameTexture.SetPixel(frame.Width - 1 - x, y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped
    30. }
    31. frameTexture.Apply();
    32. gifFrames.Add(frameTexture);
    33. }
    34. }
    35.  
    36. void OnGUI()
    37. {
    38. GUI.DrawTexture(new Rect(drawPosition.x, drawPosition.y, gifFrames[0].width, gifFrames[0].height), gifFrames[(int)(Time.frameCount * speed) % gifFrames.Count]);
    39. }
    40. }
    These were the instructions and what i tried in //

    1) Copy the "System.Drawing.dll" file in the "C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0" folder into your "Assets" folder..//DONE

    2) Create a new script named "AnimatedGifDrawer", with the contents shown below.//DONE

    3) Attach this script to any object in your scene.//DONE attached to empty game object

    4) Change the "loadingGifPath" field of the script, (in the Inspector view), to the path of your Gif file. (this can be relative, from the root project folder (i.e. the parent of the "Assets" folder), or absolute)
    // I have dowloaded a gif called water then imported it to unity just into assets. I have saved unity project "test" to my c drive. so the water gif is in Asset "test" on the c drive. .
    The error i get

    FileNotFoundException: water
    System.Drawing.Image.FromFile (System.String filename, Boolean useEmbeddedColorManagement)
    System.Drawing.Image.FromFile (System.String filename)
    AnimatedGifDrawer.Awake () (at Assets/AnimatedGifDrawer.cs:15)

    I tried just putting the name of the gif water in the field tried \Assets\water c:\test\Assets\water

    Do i need to do something to the gif first am I using a wrong path?

    Thx for any help
     
    Last edited: Sep 29, 2014
  2. Starkiller1086

    Starkiller1086

    Joined:
    Feb 17, 2015
    Posts:
    1
    []=Sorry to say mate, but I have no idea what that means. Can't help you with it at all.=[]
     
  3. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    You need to have full filename for the file but rather that doing that I suggest you change the whole thing cause reasons like: gif format is unsupported in unity, adding the support via copying version specific dll files is not practical and does not work well on all platforms, etc.

    export all the frames out of the gif with some tool and animate them as normal sprite animation
     
    Kiwasi likes this.
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'd also suggest avoiding gifs and using one of the natively supported formats for animations.
     
  6. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    Split gif into frames then animate them using Unity's animator. There is no need for rocket science.
     
  7. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    you guys do know this thread was 8 months old lol way to necro it.
     
  8. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    So how did it turn out? Did you abandon the project or solved the problem?
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    HeHe. Didn't even notice it.
     
  10. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Yea someone pointed me to a script that worked great . Can't sem to find it now. But i saw a tutorial on how to use water pro features in basic so switched to that can't use reflection refractions etc but the rest works in indy.