EchoTrio
 
Loading...
Searching...
No Matches
SpriteSwitcher.cs
Go to the documentation of this file.
1using UnityEngine;
2
3namespace EchoTrio.UI {
4 [RequireComponent(typeof(UnityEngine.UI.Image))]
5 public class SpriteSwitcher : MonoBehaviour {
6 [SerializeField] private Sprite[] sprites = new Sprite[0];
7 [SerializeField] private int index = 0;
8
9 public void CycleSprite() {
10 index = (index + 1) % sprites.Length;
11 GetComponent<UnityEngine.UI.Image>().sprite = sprites[index];
12 }
13
14 public void SetSprite(int index) {
15 this.index = index;
16 GetComponent<UnityEngine.UI.Image>().sprite = sprites[index];
17 }
18
19 private void OnValidate() {
20 UnityEngine.UI.Image image = GetComponent<UnityEngine.UI.Image>();
21 if (image != null && sprites[0] != null) {
22 image.sprite = sprites[0];
23 } else {
24 image.sprite = null;
25 }
26 }
27 }
28}