[UGUI]帧动画

ImageFrameAnimation.cs

  1 using System.Collections.Generic;
  2 using UnityEngine;
  3 using UnityEngine.UI;
  4 
  5 [RequireComponent(typeof(Image))]
  6 public class ImageFrameAnimation : MonoBehaviour {
  7 
  8     private Image image;
  9     private int nowFrame = 0;
 10     private float timer = 0;
 11 
 12     private List<Sprite> spriteList;
 13     private bool isPlay = false;
 14     private bool isForward = true;
 15     private bool isLoop = true;
 16     private float interval = 0.1f;
 17     
 18     void Update ()
 19     {
 20         if (!isPlay || (spriteList.Count == 0))
 21         {
 22             return;
 23         }
 24 
 25         timer += Time.deltaTime;
 26         if (timer > interval)
 27         {
 28             timer = 0;
 29             if (isForward)
 30             {
 31                 nowFrame++;
 32             }
 33             else
 34             {
 35                 nowFrame--;
 36             }
 37 
 38             if (nowFrame >= spriteList.Count)
 39             {
 40                 if (isLoop)
 41                 {
 42                     nowFrame = 0;
 43                 }
 44                 else
 45                 {
 46                     isPlay = false;
 47                     return;
 48                 }
 49             }
 50             else if(nowFrame < 0)
 51             {
 52                 if (isLoop)
 53                 {
 54                     nowFrame = spriteList.Count - 1;
 55                 }
 56                 else
 57                 {
 58                     isPlay = false;
 59                     return;
 60                 }
 61             }
 62 
 63             UpdateSprite();
 64         }
 65     }
 66 
 67     private void UpdateSprite()
 68     {
 69         UpdateSprite(nowFrame);
 70     }
 71 
 72     private void UpdateSprite(int index)
 73     {
 74         image.sprite = spriteList[index];
 75     }
 76 
 77     public void Init(Sprite[] spriteArray, bool isPlay = true, bool isForward = true, bool isLoop = true, float interval = 0.1f)
 78     {
 79         List<Sprite> spriteList = new List<Sprite>();
 80         spriteList.AddRange(spriteArray);
 81         Init(spriteList, isPlay, isForward, isLoop, interval);
 82     }
 83 
 84     public void Init(List<Sprite> spriteList, bool isPlay = true, bool isForward = true, bool isLoop = true, float interval = 0.1f)
 85     {
 86         this.spriteList = spriteList;
 87         this.isPlay = isPlay;
 88         this.isForward = isForward;
 89         this.isLoop = isLoop;
 90         this.interval = interval;
 91 
 92         if (image == null)
 93         {
 94             image = GetComponent<Image>();
 95         }
 96         if (isForward)
 97         {
 98             nowFrame = 0;
 99         }
100         else
101         {
102             nowFrame = spriteList.Count - 1;
103         }
104         timer = 0;
105 
106         UpdateSprite();
107     }
108 
109     public void Play()
110     {
111         isPlay = true;
112         isForward = true;
113     }
114 
115     public void PlayReverse()
116     {
117         isPlay = true;
118         isForward = false;
119     }
120 
121     public void Pause()
122     {
123         isPlay = false;
124     }
125 }

测试:

 1 using UnityEngine;
 2 using System.Collections.Generic;
 3 using UnityEngine.UI;
 4 
 5 public class TestImageFrameAnimation : MonoBehaviour {
 6 
 7     public Image image;
 8     private ImageFrameAnimation ani;
 9 
10     void Start ()
11     {
12         ani = image.gameObject.AddComponent<ImageFrameAnimation>();
13         Sprite[] sprites = Resources.LoadAll<Sprite>("Num");
14         ani.Init(sprites, true, true, true, 1);
15     }
16 
17     private void Update()
18     {
19         if (Input.GetKeyDown(KeyCode.Q))
20         {
21             ani.Pause();
22         }
23 
24         if (Input.GetKeyDown(KeyCode.W))
25         {
26             ani.PlayReverse();
27         }
28     }
29 }

效果:

猜你喜欢

转载自www.cnblogs.com/lyh916/p/9194823.html