少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using UnityEngine;
 
namespace CJ.Wait
{
    /// <summary>
    /// 延迟几秒执行事件
    /// </summary>
    public class WaitCallBack : MonoBehaviour
    {
        internal float waitTime = 2.0f;
 
        public bool doOnAwake = false;
        /// <summary>
        /// 时间到达回调
        /// </summary>
        public Action<Component> OnCompelete;
        /// <summary>
        /// 每帧回调
        /// </summary>
        public Action<Component> OnWait;
 
        internal static Dictionary<GameObject, WaitCallBack> waitDic = new Dictionary<GameObject, WaitCallBack>();
 
        private float m_Time = 0;
 
        private bool start = false;
 
        private Component m_Comp;
 
        private void Awake()
        {
            if (doOnAwake)
            {
                Play();
            }
        }
 
        private void OnDisable()
        {
            Stop();
        }
 
        private void Update()
        {
            if (start)
            {
                m_Time += Time.deltaTime;
                if (m_Time >= waitTime)
                {
                    Stop();
                    if (OnCompelete != null)
                        OnCompelete(m_Comp);
                }
                if (OnWait != null && start)
                    OnWait(m_Comp);
            }
        }
 
        internal void Play()
        {
            start = true;
        }
 
        internal void Restart()
        {
            m_Time = 0;
            Play();
        }
 
        internal void Stop()
        {
            m_Time = 0;
            start = false;
        }
 
        internal void Pause()
        {
            start = false;
        }
 
        private void OnDestroy()
        {
            if (waitDic.ContainsKey(gameObject))
            {
                waitDic.Remove(gameObject);
            }
        }
 
        internal void AddWaitDic(Component go)
        {
            m_Comp = go;
            waitDic.Add(go.gameObject, this);
        }
    }
 
    public static class Helper
    {
        public static void DoWaitStart(this Component go)
        {
            if (GetWait(go) == null)
                return;
            GetWait(go).Play();
        }
 
        public static void OnWaitCompelete(this Component go, Action<Component> func)
        {
            WaitCallBack wait = GetWait(go);
            if (wait == null)
                return;
            wait.OnCompelete = func;
        }
 
        public static void OnWait(this Component go, Action<Component> func)
        {
            WaitCallBack wait = GetWait(go);
            if (wait == null)
                return;
            wait.OnWait = func;
        }
 
        public static void DoWaitRestart(this Component go)
        {
            if (GetWait(go) == null)
                return;
            GetWait(go).Restart();
        }
 
        public static void DoWaitStop(this Component go)
        {
            if (GetWait(go) == null)
                return;
            GetWait(go).Stop();
        }
 
        public static void DoWaitPause(this Component go)
        {
            if (GetWait(go) == null)
                return;
            GetWait(go).Pause();
        }
 
        public static WaitCallBack GetWait(Component go)
        {
            WaitCallBack wait = null;
            if (go == null)
                return null;
            WaitCallBack.waitDic.TryGetValue(go.gameObject, out wait);
            if (wait == null)
            {
                wait = go.GetComponent<WaitCallBack>();
                if (wait == null)
                {
                    wait = go.gameObject.AddComponent<WaitCallBack>();
                }
                wait.AddWaitDic(go);
            }
            return wait;
        }
 
        public static void SetWait(this Component comp, float time)
        {
            WaitCallBack wait = GetWait(comp);
            if (wait != null)
            {
                wait.waitTime = time;
            }
        }
    }
}