summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Rendering/SpriteAnimationController.cs
blob: 3c4bf2b50e49be73576fbc57087c90bc57b8fafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace WK.Rendering
{

    public class SpriteAnimationController : MonoBehaviour
    {
        #region 序列化
        [SerializeField] private SpriteAnimation m_SpriteAnimation;
        [SerializeField] private bool m_AutoPlay;
        #endregion

        #region 公共字段
        public bool playing
        {
            get
            {
                return m_IsPlaying;
            }
            set
            {
                m_IsPlaying = value;
            }
        }
        #endregion

        #region 私有字段
        private SpriteRenderer m_SpriteRenderer;
        private bool m_IsPlaying;
        #endregion

        private void Awake()
        {
            m_SpriteRenderer = GetComponent<SpriteRenderer>();

            StartCoroutine(CoPlayAnimation(m_SpriteAnimation.duration));

            m_IsPlaying = true;


            m_SpriteRenderer.sprite = m_SpriteAnimation.sprites[0];
        }

        IEnumerator CoPlayAnimation(float duration = 1f)
        {
            int index = 0;
            while (true)
            {
                if (!playing)
                {
                    yield return null;
                    continue;
                }

                m_SpriteRenderer.sprite = m_SpriteAnimation.sprites[index];
                yield return new WaitForSeconds(m_SpriteAnimation.duration / m_SpriteAnimation.sprites.Count);
                index++;
                index %= m_SpriteAnimation.sprites.Count;   
            }
        }

    }

}