diff --git a/UnityGame/Assets/Scripts/GameTools/Coroutine.meta b/UnityGame/Assets/Scripts/GameTools/Coroutine.meta new file mode 100644 index 0000000..79cb860 --- /dev/null +++ b/UnityGame/Assets/Scripts/GameTools/Coroutine.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 05649470a3a64898bac0e32fd05aac14 +timeCreated: 1753497036 \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameTools/Coroutine/CoroutineManager.cs b/UnityGame/Assets/Scripts/GameTools/Coroutine/CoroutineManager.cs new file mode 100644 index 0000000..00ff4c1 --- /dev/null +++ b/UnityGame/Assets/Scripts/GameTools/Coroutine/CoroutineManager.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace CreatGame +{ + public class CoroutineManager : MonoBehaviour + { + #region Instance + private static CoroutineManager _instance; + + public static CoroutineManager Instance + { + get + { + if (_instance == null) + { + var gameObj = new GameObject("CoroutineManager"); + _instance = gameObj.AddComponent(); + DontDestroyOnLoad(gameObj); + } + return _instance; + } + } + + #endregion + + private ulong coroutineId = 0; + private ulong GetNextId() + { + return ++coroutineId; + } + + private Dictionary coroutines = new Dictionary(); + + public ulong StartC(IEnumerator routine) + { + var id = GetNextId(); + var coroutine = StartCoroutine(routine); + coroutines.Add(id, coroutine); + return id; + } + /// + /// + /// + /// 间隔时间 + /// 是否立即执行 + /// 是否循环 + /// 方法 + public ulong StartC(float time,bool isFirst , bool loop, Action action) + { + var id = StartC(LoopCoroutine(time,isFirst,loop,action)); + return id; + } + /// + /// + /// + /// + private IEnumerator LoopCoroutine(float time,bool isFirst ,bool loop, Action action) + { + if (isFirst) + { + action.Invoke(); + } + + while (true) + { + yield return new WaitForSeconds(time); + action.Invoke(); + if (!loop) + { + break; + } + } + } + + public void StopC(ulong id) + { + if (coroutines.ContainsKey(id)) + { + StopCoroutine(coroutines[id]); + coroutines.Remove(id); + } + } + + private void OnDestroy() + { + + } + } +} \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameTools/Coroutine/CoroutineManager.cs.meta b/UnityGame/Assets/Scripts/GameTools/Coroutine/CoroutineManager.cs.meta new file mode 100644 index 0000000..4404939 --- /dev/null +++ b/UnityGame/Assets/Scripts/GameTools/Coroutine/CoroutineManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7089d863c3ed4fb0bc6d7ddcb8488817 +timeCreated: 1753497047 \ No newline at end of file