Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by navadeep2011 · Apr 02, 2013 at 04:33 AM · guitexturesguitextureimagesgif

How will i get animated gif images in scene?

Hi All,

I don't know unity supports animated gif images are not.

But i have one scene. When we open that scene i want some animated gif image to display. how will i do that.

i tired unity GUITextures,2dTextures but i didn't get any thing. Only pain texture came. not performing any operation.

please give a suggestion so that i will implement that

Thank you

Shankar

alt text

output_dz0jev.gif (82.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by cybergoogle · Jul 02, 2015 at 08:08 PM

var frames : Texture2D[]; var framesPerSecond = 10.0;

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cybergoogle · Jul 02, 2015 at 08:09 PM 1
Share

add that script to a plane or whatever object you want to apply it to and then assign how many sprites you want to use and the frame length in the inspector.

avatar image raszop · Apr 19, 2016 at 12:41 AM 1
Share

thank you for posting this. very simple yet effective solution.

avatar image JorgeAmVF · Mar 14, 2018 at 09:29 AM 1
Share

Precise solution.

avatar image Ianaa · Apr 29, 2020 at 11:05 AM 0
Share

What is this in C#?

avatar image
5

Answer by orrinjones · Feb 17, 2017 at 08:24 AM

I see you have already gotten an answer however I would just like to add that instead of using an array you could simply use the animation editor and Change the sprite property every Frame. Then use an animator controller to specify when it is to be played how fast and so on.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BadSeedProductions · Jul 29, 2017 at 04:48 PM 1
Share

You can also export the gif animation to individual frames. $$anonymous$$ake sure names of files are sequenced like so: file001, file002, file003 etc. Import them into unity as sprites. Drag and drop entire sequence into scene and animator controller and animation will be automatically generated.

avatar image
4

Answer by Bryan-Legend · Jul 26, 2017 at 06:05 PM

Convert it to an .mp4 and use movie textures.

https://docs.unity3d.com/Manual/class-MovieTexture.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by juggernaut5k · Feb 01, 2020 at 12:09 AM

Here's an answer that takes a bit up front but allows you to work with the GIF directly without having to save individual files to disk and what not. It will also play it back at the correct speed by reading the per frame delay from the Gif file. Might be a better way out there but I haven't found it without paying for it. Hopefully it works for you.


First you need to get the System.Drawing.dll and drop it into your Assets folder. For whatever reason it's not available in the Unity default environment but is required to use that specific Image class. That file is likely located in the following location.


C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Drawing.dll


Create a new text file in the Assets folder and call that file mcs.rsp. Now add this text to that file and save it.

 -r:System.Drawing.dll



Now create a new Gif.cs file and put the following code in it.

 using System.Collections.Generic;
 using System.Drawing;
 using System;
 using System.Drawing.Imaging;
 using System.Runtime.InteropServices;
 using UnityEngine;
 
 public class Gif : MonoBehaviour
 {
     public Image gifImage;
     public float delay = 0.1f;
 
     int frameCount = 0;
     FrameDimension dimension;
 
     public void loadGif(string filepath)
     {
         gifImage = Image.FromFile(filepath);
         dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
         frameCount = gifImage.GetFrameCount(dimension);
     }
 
     private static byte[] Bitmap2RawBytes(Bitmap bmp)
     {
         byte[] bytes;
         byte[] copyToBytes;
         BitmapData bitmapData;
         IntPtr Iptr = IntPtr.Zero;
 
         bytes = new byte[bmp.Width * bmp.Height * 4];
         copyToBytes = new byte[bmp.Width * bmp.Height * 4];
 
         bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
         Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
         bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
         Iptr = bitmapData.Scan0;
         Marshal.Copy(Iptr, bytes, 0, bytes.Length);
 
         for (int i = 0; i < bytes.Length; i++)
         {
             copyToBytes[bytes.Length - 1 - i] = bytes[i];
         }
         bmp.UnlockBits(bitmapData);
 
         return copyToBytes;
     }
 
     public List<Texture2D> GetFrames()
     {
         List<Texture2D> gifFrames = new List<Texture2D>(frameCount);
         for (int i = 0; i < frameCount; i++)
         {
             gifImage.SelectActiveFrame(dimension, i);
             PropertyItem item = gifImage.GetPropertyItem(0x5100);
             int frameDelay = (item.Value[0] + item.Value[1] * 256) * 10;
             delay = frameDelay / 1000f;
             var frame = new Bitmap(gifImage.Width, gifImage.Height);
             System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
             Texture2D texture = new Texture2D(frame.Width, frame.Height, TextureFormat.ARGB32, false);
             texture.LoadRawTextureData(Bitmap2RawBytes(frame));
             texture.Apply();
             gifFrames.Add(texture);
         }
         return gifFrames;
     }
 }
 



You can now use the files by doing something like this. Create an UI-->Image element. Place the following code in a new script called GifImageDraw.cs. Place that script on some game object. Then by pressing "G" it will display the animated GIF 3 times in a row.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;
 
 public class GifImageDraw : MonoBehaviour
 {
     public string gifPath = "";
     // This is the UI image object you created which is a child of "canvas".
     public GameObject imageGo;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.G))
         {
             GrabGifImage();
         }
     }
     void GrabGifImage()
     {
         ShowGif(gifPath);
     }
 
     void ShowGif(string path)
     {
         GameObject imageGo = GameObject.Find("RawImage");
         Gif gif = new Gif();
         gif.loadGif(path);
         RectTransform rect = imageGo.GetComponent<RectTransform>();
         List<Texture2D> frames = gif.GetFrames();
         RawImage rawImage = imageGo.GetComponent<RawImage>();
         StartCoroutine(ShowGifFrames(rawImage, frames, gif.delay));
     }
 
     IEnumerator ShowGifFrames(RawImage rawImage, List<Texture2D> frames, float delay)
     {
         if (delay < 0.05f)
             delay = 0.05f;
         // Go for 5 iterations for clarity
         for (int i = 0; i < 3; i++)
         {
             foreach (Texture2D frame in frames)
             {
                 rawImage.texture = frame as Texture;
                 yield return new WaitForSeconds(delay);
             }
         }
     }
 }



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 01, 2020 at 02:29 AM 0
Share

Well, the main reason why Unity most likely does not include the System.Drawing.dll is because it's not platform independent. Almost all classes in that assembly are just wrapper classes around native GDI / GDI+ objects. Note that even if the platform binding is not an issue, the System.Drawing dll is quite large. I've actually written a GIF loader from scratch, however it's not quite user friendly yet. It's not finished but it can already load most gif images. It's just a single C# source code file with only about 900 lines of code.

avatar image juggernaut5k · Feb 01, 2020 at 02:12 PM 0
Share

Sounds good, @Bunny83. Was just trying to get some code out there for those in need. I spent a while looking and couldn't find a complete solution without writing out a bunch of images or buying some asset in the Unity asset store. Looking forward to seeing your solution out there. Planning on it being in Github? Also, the System.Drawing.dll is only about 200$$anonymous$$B at least in my project. Nothing substantial.

avatar image
0

Answer by ShinyTaco · Apr 02, 2013 at 04:39 AM

http://forum.unity3d.com/threads/10126-Animated-GIFS

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image navadeep2011 · Apr 02, 2013 at 04:42 AM 0
Share

i searched and tired that one but i am not getting any correct output.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to show an image 2 Answers

Chat Client in Unity 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

GUI texture touch input problem 1 Answer

Relative GUI positioning and Pixel Inset/Offset conversion 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges