Last active
December 6, 2022 21:02
-
-
Save R3tr0BoiDX/d84e91a03d020f2169d92446f1ffb857 to your computer and use it in GitHub Desktop.
Easy animator for simple sprite animations. Use best with other gist "SimpleAnimatorEditor.cs". PREVIEW: https://github.com/R3tr0BoiDX/R3tr0BoiDX/blob/main/SimpleAnimator.png
This file contains hidden or 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; | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace Tools { | |
/// <summary> | |
/// Easy animator for simple sprite animations | |
/// Theres also a editor integration, you can find here: https://gist.github.com/R3tr0BoiDX/1607500b036531e17dadaa7e6255818d | |
/// </summary> | |
public class SimpleAnimator : MonoBehaviour { | |
[SerializeField] private bool guiMode; | |
[SerializeField] private bool startAtSpawn = true; | |
[SerializeField] private bool loopAnimation; | |
[SerializeField] private bool destroyAfterFinished; | |
[SerializeField] private float destroyDelay; | |
[SerializeField] private float timeBetweenFrames; | |
[SerializeField] private Sprite[] frames; | |
private Task animator; //TODO: Get from here: https://gist.github.com/R3tr0BoiDX/1f58d5c2285ba2daf7cd13ac7f67f95e | |
private int currentSpriteIndex; | |
private Image image; | |
private SpriteRenderer rend; | |
public Sprite[] Frames => frames; | |
public float TimeBetweenFrames => timeBetweenFrames; | |
public bool UseGUIMode => guiMode; | |
private void Start() { | |
if (guiMode) { | |
image = GetComponent<Image>(); | |
} else { | |
rend = GetComponent<SpriteRenderer>(); | |
} | |
if (startAtSpawn) { | |
animator = new Task(Animate()); | |
} | |
} | |
public void Play() { | |
if (animator != null) { | |
animator.Unpause(); | |
} else { | |
animator = new Task(Animate()); | |
} | |
} | |
public void Stop() { | |
if (animator != null) { | |
animator.Stop(); | |
SetSprite(frames[0]); | |
} | |
} | |
public void Pause() { | |
animator?.Pause(); | |
} | |
/// <summary> | |
/// Main | |
/// </summary> | |
/// <returns></returns> | |
private IEnumerator Animate() { | |
SetSprite(frames[currentSpriteIndex]); | |
currentSpriteIndex++; | |
yield return new WaitForSeconds(timeBetweenFrames); | |
if (currentSpriteIndex < frames.Length) { | |
//Start a new enumerator for next index | |
animator = new Task(Animate()); | |
} else if (loopAnimation) { | |
//Reset index and start over again | |
currentSpriteIndex = 0; | |
animator = new Task(Animate()); | |
} else if (destroyAfterFinished) { | |
//Animation played and object is supposed to get destroyed | |
Destroy(gameObject, destroyDelay); | |
} | |
} | |
/// <summary> | |
/// Sets the sprite of the selected render method. | |
/// When in GUI mode, then the sprite of the Unity GUI Image component gets replaced | |
/// </summary> | |
/// <param name="_sprite">The new sprite, that's supposed to be shown</param> | |
private void SetSprite(Sprite _sprite) { | |
if (guiMode) { | |
image.sprite = _sprite; | |
} else { | |
rend.sprite = _sprite; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment