- Home /
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
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]; }
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.
thank you for posting this. very simple yet effective solution.
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.
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.
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);
}
}
}
}
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.
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.
Answer by ShinyTaco · Apr 02, 2013 at 04:39 AM
i searched and tired that one but i am not getting any correct output.
Your answer
Follow this Question
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