协程工具
This commit is contained in:
3
UnityGame/Assets/Scripts/GameTools/Coroutine.meta
Normal file
3
UnityGame/Assets/Scripts/GameTools/Coroutine.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 05649470a3a64898bac0e32fd05aac14
|
||||||
|
timeCreated: 1753497036
|
||||||
@@ -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<CoroutineManager>();
|
||||||
|
DontDestroyOnLoad(gameObj);
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private ulong coroutineId = 0;
|
||||||
|
private ulong GetNextId()
|
||||||
|
{
|
||||||
|
return ++coroutineId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<ulong,Coroutine> coroutines = new Dictionary<ulong,Coroutine>();
|
||||||
|
|
||||||
|
public ulong StartC(IEnumerator routine)
|
||||||
|
{
|
||||||
|
var id = GetNextId();
|
||||||
|
var coroutine = StartCoroutine(routine);
|
||||||
|
coroutines.Add(id, coroutine);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="time">间隔时间</param>
|
||||||
|
/// <param name="isFirst">是否立即执行</param>
|
||||||
|
/// <param name="loop">是否循环</param>
|
||||||
|
/// <param name="action">方法</param>
|
||||||
|
public ulong StartC(float time,bool isFirst , bool loop, Action action)
|
||||||
|
{
|
||||||
|
var id = StartC(LoopCoroutine(time,isFirst,loop,action));
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7089d863c3ed4fb0bc6d7ddcb8488817
|
||||||
|
timeCreated: 1753497047
|
||||||
Reference in New Issue
Block a user