summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Common/EffectPlayer.cs
blob: f60165a0bc34dbc9ee089f9c6c4aa2986f16971f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using UnityEngine;
using System;
using System.Collections.Generic;

namespace Coffee.UIEffects
{
    /// <summary>
    /// Effect player.
    /// </summary>
    [Serializable]
    public class EffectPlayer
    {
        //################################
        // Public Members.
        //################################
        /// <summary>
        /// Gets or sets a value indicating whether is playing.
        /// </summary>
        [Header("Effect Player")] [Tooltip("Playing.")]
        public bool play = false;

        /// <summary>
        /// Gets or sets the delay before looping.
        /// </summary>
        [Tooltip("Initial play delay.")] [Range(0f, 10f)]
        public float initialPlayDelay = 0;

        /// <summary>
        /// Gets or sets the duration.
        /// </summary>
        [Tooltip("Duration.")] [Range(0.01f, 10f)]
        public float duration = 1;

        /// <summary>
        /// Gets or sets a value indicating whether can loop.
        /// </summary>
        [Tooltip("Loop.")] public bool loop = false;

        /// <summary>
        /// Gets or sets the delay before looping.
        /// </summary>
        [Tooltip("Delay before looping.")] [Range(0f, 10f)]
        public float loopDelay = 0;

        /// <summary>
        /// Gets or sets the update mode.
        /// </summary>
        [Tooltip("Update mode")] public AnimatorUpdateMode updateMode = AnimatorUpdateMode.Normal;

        static List<Action> s_UpdateActions;

        /// <summary>
        /// Register player.
        /// </summary>
        public void OnEnable(Action<float> callback = null)
        {
            if (s_UpdateActions == null)
            {
                s_UpdateActions = new List<Action>();
                Canvas.willRenderCanvases += () =>
                {
                    var count = s_UpdateActions.Count;
                    for (int i = 0; i < count; i++)
                    {
                        s_UpdateActions[i].Invoke();
                    }
                };
            }

            s_UpdateActions.Add(OnWillRenderCanvases);

            if (play)
            {
                _time = -initialPlayDelay;
            }
            else
            {
                _time = 0;
            }

            _callback = callback;
        }

        /// <summary>
        /// Unregister player.
        /// </summary>
        public void OnDisable()
        {
            _callback = null;
            s_UpdateActions.Remove(OnWillRenderCanvases);
        }

        /// <summary>
        /// Start playing.
        /// </summary>
        public void Play(bool reset, Action<float> callback = null)
        {
            if (reset)
            {
                _time = 0;
            }

            play = true;
            if (callback != null)
            {
                _callback = callback;
            }
        }

        /// <summary>
        /// Stop playing.
        /// </summary>
        public void Stop(bool reset)
        {
            if (reset)
            {
                _time = 0;
                if (_callback != null)
                {
                    _callback(_time);
                }
            }

            play = false;
        }

        //################################
        // Private Members.
        //################################
        float _time = 0;
        Action<float> _callback;

        void OnWillRenderCanvases()
        {
            if (!play || !Application.isPlaying || _callback == null)
            {
                return;
            }

            _time += updateMode == AnimatorUpdateMode.UnscaledTime
                ? Time.unscaledDeltaTime
                : Time.deltaTime;
            var current = _time / duration;

            if (duration <= _time)
            {
                play = loop;
                _time = loop ? -loopDelay : 0;
            }

            _callback(current);
        }
    }
}