Last active
October 20, 2020 15:07
-
-
Save shukerullah/70e7536774cf91fafb44e2a104480672 to your computer and use it in GitHub Desktop.
Unity (Canvas) UI.Image Sprite Animation
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 UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Events; | |
[RequireComponent(typeof(Image))] | |
public class UISpriteAnimation : MonoBehaviour { | |
public Sprite[] sprites; | |
public int fps = 6; | |
public bool playOnAwake = true; | |
public bool loop = true; | |
public bool destroyOnEnd = false; | |
public UnityEvent onAnimationComplete; | |
private bool m_Play = false; | |
private int m_Index = 0; | |
private Image m_Image; | |
private int m_Frame = 0; | |
void Awake() { | |
m_Image = GetComponent<Image> (); | |
PlayOnAwake(); | |
} | |
public void Play() { | |
EnableImage(); | |
m_Index = 1; | |
m_Play = true; | |
} | |
public void Stop() { | |
DisableImage(); | |
m_Index = 0; | |
m_Play = false; | |
} | |
void Update () { | |
if (!m_Play) { | |
return; | |
} | |
m_Frame ++; | |
if (m_Frame < fps) { | |
return; | |
} | |
m_Frame = 0; | |
if (m_Index >= sprites.Length) { | |
OnAnimationComplete(); | |
} | |
m_Image.sprite = sprites [m_Index]; | |
m_Index ++; | |
} | |
void OnAnimationComplete() { | |
m_Index = 0; | |
onAnimationComplete.Invoke(); | |
if(destroyOnEnd) { | |
Destroy(gameObject); | |
} else if(!loop) { | |
Stop(); | |
} | |
} | |
void PlayOnAwake() { | |
if(!playOnAwake) { | |
DisableImage(); | |
} else { | |
Play(); | |
} | |
} | |
void EnableImage() { | |
if(!m_Image.enabled) { | |
m_Image.enabled = true; | |
} | |
} | |
void DisableImage() { | |
if(m_Image.enabled) { | |
m_Image.enabled = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Public Methods