Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Created November 20, 2014 01:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mstevenson/d5098bbaabe512a9f416 to your computer and use it in GitHub Desktop.
Save mstevenson/d5098bbaabe512a9f416 to your computer and use it in GitHub Desktop.
2D sprite animator for Unity
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[RequireComponent (typeof(SpriteRenderer))]
public class SpriteAnimator : MonoBehaviour
{
public List<Sprite> frames = new List<Sprite> ();
public float fps = 15;
public bool playOnStart;
public bool loop;
SpriteRenderer rend;
int currentFrame;
float playStartTime;
public bool IsPlaying { get; private set; }
void Awake ()
{
rend = GetComponent<SpriteRenderer> ();
}
void Start ()
{
if (playOnStart) {
Play ();
}
}
public void Play ()
{
if (frames.Count == 0) {
return;
}
StartCoroutine ("DoPlay");
}
IEnumerator DoPlay ()
{
IsPlaying = true;
playStartTime = Time.time;
currentFrame = 0;
while (true) {
if (fps < 1) {
yield return null;
}
var t = (Time.time - playStartTime) * fps;
if (loop || (!loop && t < frames.Count)) {
SetFrame (Mathf.FloorToInt (t));
}
yield return null;
}
}
public void Stop ()
{
IsPlaying = false;
SetFrame (0);
StopAllCoroutines ();
playStartTime = 0;
}
public void SetFrame (int frame)
{
currentFrame = frame % frames.Count;
rend.sprite = frames[currentFrame];
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[CustomEditor (typeof(SpriteAnimator))]
public class SpriteAnimatorEditor : Editor
{
// Example code:
// https://gist.github.com/bzgeb/3800350
SerializedObject obj;
void OnEnable ()
{
obj = new SerializedObject (target);
}
public override void OnInspectorGUI ()
{
DrawDefaultInspector ();
EditorGUILayout.Space ();
if (GUILayout.Button ("Sort Frames")) {
((SpriteAnimator)obj.targetObject).frames = ((SpriteAnimator)obj.targetObject).frames.OrderBy (s => s.name).ToList ();
}
DropAreaGUI ();
}
public void DropAreaGUI ()
{
Event evt = Event.current;
Rect dropArea = GUILayoutUtility.GetRect (0.0f, 50.0f, GUILayout.ExpandWidth (true));
GUI.Box (dropArea, "Add Sprites");
switch (evt.type) {
case EventType.DragUpdated:
case EventType.DragPerform:
if (!dropArea.Contains (evt.mousePosition)) {
return;
}
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (evt.type == EventType.DragPerform) {
DragAndDrop.AcceptDrag ();
foreach (Object draggedObject in DragAndDrop.objectReferences) {
if (draggedObject is Sprite) {
// Add sprites to list directly
((SpriteAnimator)obj.targetObject).frames.Add ((Sprite)draggedObject);
} else if (draggedObject is Texture) {
// Grab all sprites associated with a texture, add to list
var path = AssetDatabase.GetAssetPath (draggedObject);
var assets = AssetDatabase.LoadAllAssetRepresentationsAtPath (path);
foreach (var subAsset in assets) {
if (subAsset is Sprite) {
((SpriteAnimator)obj.targetObject).frames.Add ((Sprite)subAsset);
}
}
}
}
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment