Created
February 15, 2016 10:11
-
-
Save frideal/a43693df932d90d111bb to your computer and use it in GitHub Desktop.
GIf播放器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using UnityEngine; | |
using UnityEngine.UI; | |
//AnimatedGifDrawer | |
//The following script can be used to draw animated Gif images on-screen in Unity.It uses the "Image" and "Graphics" class from the System.Drawing namespace, and so requires the "System.Drawing.dll" file to be imported/added to the project. | |
//Instructions: | |
//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. | |
//2) Create a new script named "AnimatedGifDrawer", with the contents shown below. | |
//3) Attach this script to any object in your scene. | |
//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) | |
public class AnimatedGifDrawer : MonoBehaviour | |
{ | |
public string loadingGifPath; | |
public float speed = 1; | |
public Vector2 drawPosition; | |
List<Texture2D> gifFrames = new List<Texture2D>(); | |
public RawImage gifTexture; | |
public int perSecondCount = 30; | |
private float lastPlayTime = 0.0f; | |
void Awake() | |
{ | |
var gifImage = System.Drawing.Image.FromFile(loadingGifPath); | |
var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); | |
int frameCount = gifImage.GetFrameCount(dimension); | |
for (int i = 0; i < frameCount; i++) | |
{ | |
gifImage.SelectActiveFrame(dimension, i); | |
var frame = new Bitmap(gifImage.Width, gifImage.Height); | |
System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty); | |
var frameTexture = new Texture2D(frame.Width, frame.Height); | |
for (int x = 0; x < frame.Width; x++) | |
for (int y = 0; y < frame.Height; y++) | |
{ | |
System.Drawing.Color sourceColor = frame.GetPixel(x, y); | |
frameTexture.SetPixel(frame.Width - 1 - x, y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped | |
} | |
frameTexture.Apply(); | |
gifFrames.Add(frameTexture); | |
} | |
lastPlayTime = Time.time; | |
this.gifTexture.rectTransform.sizeDelta = new Vector2(gifFrames[0].width, gifFrames[0].height); | |
} | |
void OnGUI() | |
{ | |
// GUI.DrawTexture(new Rect(drawPosition.x, drawPosition.y, gifFrames[0].width, gifFrames[0].height), gifFrames[(int)(Time.frameCount * speed) % gifFrames.Count]); | |
} | |
private int curGifFrame = 0; | |
void Update() | |
{ | |
gifTexture.texture = gifFrames[curGifFrame]; | |
if (Time.time - this.lastPlayTime >= 1.0f / this.perSecondCount) | |
{ | |
curGifFrame++; | |
if (curGifFrame >= this.gifFrames.Count) | |
curGifFrame = 0; | |
this.lastPlayTime = Time.time; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment