summaryrefslogtreecommitdiff
path: root/Assets/UI_Extension/Scripts/Animation/Tween/Editor/PlaybackTimer.cs
blob: 533fbc85d7aadbf69d1687c151198d92286e1fea (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace TweenAnimation
{
    public class PlaybackTimer
    {
        // 动画
        private TweenAnimation m_Animation;

        // 考虑了Pause的playback逻辑时间,考虑了playbackLimit
        public float time
        {
            get
            {
                float t = realTime;
                if (m_PauseRealTime != -1)
                    t = m_PauseRealTime - m_StartRealTime;
                t = m_Animation.ApplyLimit(t);
                return t;
            }
        }

        // 从调用Resume开始后的时间
        private float realTime
        {
            get
            {
                return (float)EditorApplication.timeSinceStartup - m_StartRealTime;
            }
        }

        private float m_StartRealTime;

        private float m_PauseRealTime;

        public PlaybackTimer(TweenAnimation animation)
        {
            m_Animation = animation;
            Reset();
        }

        //Start & Resume
        public void Resume()
        {
            if (m_PauseRealTime != -1)
                m_StartRealTime = (float)EditorApplication.timeSinceStartup - time;
            else
                m_StartRealTime = (float)EditorApplication.timeSinceStartup;
            m_PauseRealTime = -1;
        }

        public void Pause()
        {
            m_PauseRealTime = (float)EditorApplication.timeSinceStartup;
            float t = m_Animation.ApplyLimit(m_PauseRealTime - m_StartRealTime);
            m_PauseRealTime = m_StartRealTime + t;
        }

        public void Stop()
        {
            Reset();
        }

        public void Reset()
        {
            m_StartRealTime = -1;
            m_PauseRealTime = -1;
        }

        public void SetPauseTimeOffset(float dt)
        {
            m_PauseRealTime += dt;
        }

    }
}