[Unity UGUI序列帧]简单实现序列帧的播放 [Unity UGUI图集系统]浅谈UGUI图集使用

在使用序列帧之前需要准备好序列帧的图集,打图集的操作参考

[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 }
View Code

 根据自己的需求设置好参数,一个简单的UGUI下的序列帧播放功能就实现了

猜你喜欢

转载自www.cnblogs.com/lovewaits/p/12895372.html
今日推荐