Last active
May 24, 2024 08:29
-
-
Save almirage/e9e4f447190371ee6ce9 to your computer and use it in GitHub Desktop.
Sprite animation for UI.Image on Unity
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 UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class ImageAnimation : MonoBehaviour { | |
public Sprite[] sprites; | |
public int spritePerFrame = 6; | |
public bool loop = true; | |
public bool destroyOnEnd = false; | |
private int index = 0; | |
private Image image; | |
private int frame = 0; | |
void Awake() { | |
image = GetComponent<Image> (); | |
} | |
void Update () { | |
if (!loop && index == sprites.Length) return; | |
frame ++; | |
if (frame < spritePerFrame) return; | |
image.sprite = sprites [index]; | |
frame = 0; | |
index ++; | |
if (index >= sprites.Length) { | |
if (loop) index = 0; | |
if (destroyOnEnd) Destroy (gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing, thank you!