Skip to content

Instantly share code, notes, and snippets.

@TarasOsiris
Last active April 15, 2016 09:52
Show Gist options
  • Save TarasOsiris/3f4606bde19d1f9a9d04090d7b3cf89a to your computer and use it in GitHub Desktop.
Save TarasOsiris/3f4606bde19d1f9a9d04090d7b3cf89a to your computer and use it in GitHub Desktop.
Class for frame-by-frame Animation in Editor. Taken from Fabric Unity SDK
namespace Fabric.Internal.Editor.View.Animation
{
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
public class Driver
{
private uint invocationCount = 0;
private uint invokeAtFrame = 1;
private uint invokedAt = (uint)EditorApplication.timeSinceStartup;
private uint frameCount;
private uint frame = 0;
public uint Frame
{
get {
Reset ();
uint toRender = Invoke ();
Tick ();
return toRender;
}
}
public Driver(uint frameCount)
{
if (frameCount == 0) {
throw new Exception ("frame count cannot be 0");
}
this.frameCount = frameCount;
}
public void Tick()
{
++invocationCount;
}
private void Reset()
{
uint now = (uint)EditorApplication.timeSinceStartup;
if (invokedAt != now) {
invokedAt = now;
invokeAtFrame = OneOnZero (invocationCount / frameCount);
invocationCount = 0;
}
}
private uint Invoke()
{
if (invocationCount % invokeAtFrame == 0) {
if (frame == (frameCount - 1)) {
frame = 0;
} else {
frame += 1;
}
}
return frame;
}
private static uint OneOnZero(uint val)
{
return val == 0 ? 1 : val;
}
}
}
private static readonly Texture2D[] Spinner = {
Images.Loader.Load ("spinner_0.gif"),
Images.Loader.Load ("spinner_1.gif"),
Images.Loader.Load ("spinner_2.gif"),
Images.Loader.Load ("spinner_3.gif"),
Images.Loader.Load ("spinner_4.gif"),
Images.Loader.Load ("spinner_5.gif"),
Images.Loader.Load ("spinner_6.gif"),
Images.Loader.Load ("spinner_7.gif"),
Images.Loader.Load ("spinner_8.gif"),
Images.Loader.Load ("spinner_9.gif"),
Images.Loader.Load ("spinner_10.gif"),
Images.Loader.Load ("spinner_11.gif")
};
private static readonly View.Animation.Driver Driver = new View.Animation.Driver ((uint)Spinner.Length);
Texture2D frame = Spinner [Driver.Frame];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment