Skip to content

Instantly share code, notes, and snippets.

@almirage
Last active November 1, 2023 17:16
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save almirage/e9e4f447190371ee6ce9 to your computer and use it in GitHub Desktop.
Save almirage/e9e4f447190371ee6ce9 to your computer and use it in GitHub Desktop.
Sprite animation for UI.Image on Unity
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);
}
}
}
@almirage
Copy link
Author

2015-08-21 4 14 22

@mr-noodler
Copy link

Beautiful code!

@habbala
Copy link

habbala commented Nov 25, 2019

This is amazingly helpful!

@Pool4Sky
Copy link

Pool4Sky commented Jan 7, 2020

Perfect! Thank you.

@Dastournejad
Copy link

This is very neat thank you

@feendrache
Copy link

Thank you so much

@Mochnant
Copy link

Mochnant commented Feb 4, 2022

Thanks for the gist. Works great. I was slightly confused by the variable spitePerFrame since it seems to be more like framesPerSprite.

@fredknack
Copy link

Thanks!

@i-miss-old-mmorpg
Copy link

Brilliant

@cor2879
Copy link

cor2879 commented Aug 10, 2023

Hi, just one thought. Would it not be preferable to place this code within 'FixedUpdate' rather than update? Since we are counting frames between image updates, and Update is CPU bound you may get faster or slower animations based on the CPU of the device.

@cor2879
Copy link

cor2879 commented Aug 10, 2023

Otherwise, nice animation loop code for UI. I will be adapting this for the new UI I am building for my game on iOS

@SirMcPotato
Copy link

SirMcPotato commented Nov 1, 2023

Hi, just one thought. Would it not be preferable to place this code within 'FixedUpdate' rather than update? Since we are counting frames between image updates, and Update is CPU bound you may get faster or slower animations based on the CPU of the device.

That's also what I'll recommend to do! I've noticed that when running with Update on mobile, animation is much more slower than on desktop, because frame-rates are not the same.
Also, it should be framesPerSprite, it's confusing in the other way.

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class ImageAnimation : MonoBehaviour
{

    public Sprite[] sprites;
    public int framesPerSprite = 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 FixedUpdate()
    {
        if (!loop && index == sprites.Length)
            return;
        frame++;
        if (frame < framesPerSprite)
            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