在使用序列幀之前需要准備好序列幀的圖集,打圖集的操作參考
[Unity UGUI圖集系統]淺談UGUI圖集使用
准備好序列幀圖集,序列幀的播放原理就是獲取到圖集中的所有圖片,然后按照設置的速度按個賦值給Image,其余的都可以按照自己的需求加一些循環,是否設置原圖大小等等功能

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class UIImagAnimation : MonoBehaviour 7 { 8 public int Framerate = 10; 9 public bool IsLoop = true; 10 public bool IsSetSnap = false; 11 public string AtlasName = ""; 12 13 private Sprite[] sprites; 14 private int curIndex = 0; 15 private float curRate = 0; 16 private Image mImage; 17 // Start is called before the first frame update 18 void Start() 19 { 20 if (mImage == null) mImage = GetComponent<Image>(); 21 if (sprites == null) sprites = UIResourceLoadManager.Instance.LoadSprites(AtlasName, sprites); 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 if (mImage != null && sprites != null && sprites.Length > 0) 28 { 29 if (curIndex < sprites.Length || IsLoop) 30 { 31 curIndex %= sprites.Length; 32 curRate += Time.deltaTime; 33 if (curRate > 1) curRate = 0; 34 float tempRate = 1f / Framerate; 35 if (tempRate < curRate) 36 { 37 curRate = tempRate > 0 ? curRate - tempRate : 0; 38 mImage.sprite = sprites[curIndex]; 39 if (IsSetSnap) mImage.SetNativeSize(); 40 curIndex++; 41 } 42 } 43 } 44 } 45 }
根據自己的需求設置好參數,一個簡單的UGUI下的序列幀播放功能就實現了