提交工程
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
namespace ChillConnect
|
||||
{
|
||||
public class AsyncDealData
|
||||
{
|
||||
public delegate void DealMethod();
|
||||
|
||||
|
||||
private float _currentTime;
|
||||
|
||||
|
||||
public float DelayTime = 1f;
|
||||
|
||||
|
||||
public DealMethod dealMethod;
|
||||
|
||||
|
||||
public int RepeatCount = 1;
|
||||
|
||||
|
||||
public bool IsComplete => RepeatCount == 0;
|
||||
|
||||
|
||||
public void PassTime(float time)
|
||||
{
|
||||
_currentTime += time;
|
||||
if (!(_currentTime >= DelayTime))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_currentTime -= DelayTime;
|
||||
Run();
|
||||
}
|
||||
|
||||
|
||||
private void Run()
|
||||
{
|
||||
if (RepeatCount == -1)
|
||||
{
|
||||
dealMethod?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (RepeatCount > 0)
|
||||
{
|
||||
dealMethod?.Invoke();
|
||||
RepeatCount--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e51ff91679a44d1b1efe3438d508e74
|
||||
timeCreated: 1682577036
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ChillConnect
|
||||
{
|
||||
internal class AsyncKitUnityBehaviour : SingletonUnity<AsyncKitUnityBehaviour>
|
||||
{
|
||||
private readonly Dictionary<string, AsyncDealData> _asyncDataDict = new Dictionary<string, AsyncDealData>();
|
||||
|
||||
|
||||
private readonly Dictionary<string, AsyncDealData> _updateActionDict = new Dictionary<string, AsyncDealData>();
|
||||
|
||||
private readonly List<string> _keyClean = new List<string>();
|
||||
|
||||
private void Update()
|
||||
{
|
||||
foreach (var temp in _asyncDataDict.Values)
|
||||
{
|
||||
temp.PassTime(Time.deltaTime);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var temp in _updateActionDict.Values)
|
||||
{
|
||||
temp?.dealMethod?.Invoke();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ClearFinished();
|
||||
|
||||
|
||||
ClearUpdateActionFinished();
|
||||
}
|
||||
|
||||
|
||||
private void ClearFinished()
|
||||
{
|
||||
_keyClean.Clear();
|
||||
foreach (var dealData in _asyncDataDict)
|
||||
{
|
||||
if (dealData.Value.IsComplete)
|
||||
{
|
||||
_keyClean.Add(dealData.Key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var key in _keyClean)
|
||||
{
|
||||
_asyncDataDict.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void AddAsyncData(string key, AsyncDealData asyncDealData)
|
||||
{
|
||||
RemoveAsyncData(key);
|
||||
_asyncDataDict.Add(key, asyncDealData);
|
||||
}
|
||||
|
||||
|
||||
public void RemoveAsyncData(string key)
|
||||
{
|
||||
if (_asyncDataDict.ContainsKey(key))
|
||||
{
|
||||
_asyncDataDict.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearUpdateActionFinished()
|
||||
{
|
||||
_keyClean.Clear();
|
||||
foreach (var dealData in _updateActionDict)
|
||||
{
|
||||
if (dealData.Value.IsComplete)
|
||||
{
|
||||
_keyClean.Add(dealData.Key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var key in _keyClean)
|
||||
{
|
||||
_updateActionDict.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 984d18c5a4974e729d72276896426930
|
||||
timeCreated: 1682577036
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ChillConnect
|
||||
{
|
||||
public static class CrazyAsyKit
|
||||
{
|
||||
private static AsyncKitUnityBehaviour sAsyncKitUnityBehaviour;
|
||||
|
||||
|
||||
private static readonly object SynClock = new object();
|
||||
|
||||
private static AsyncKitUnityBehaviour mAsyncKitUnityBehaviour
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sAsyncKitUnityBehaviour != null)
|
||||
{
|
||||
return sAsyncKitUnityBehaviour;
|
||||
}
|
||||
|
||||
lock (SynClock)
|
||||
{
|
||||
if (sAsyncKitUnityBehaviour != null)
|
||||
{
|
||||
return sAsyncKitUnityBehaviour;
|
||||
}
|
||||
|
||||
|
||||
sAsyncKitUnityBehaviour = AsyncKitUnityBehaviour.Instance;
|
||||
if (sAsyncKitUnityBehaviour != null)
|
||||
{
|
||||
sAsyncKitUnityBehaviour.name = "[ CrazyAsyKit ]";
|
||||
}
|
||||
}
|
||||
|
||||
return sAsyncKitUnityBehaviour;
|
||||
}
|
||||
}
|
||||
|
||||
public static Coroutine StartCoroutine(IEnumerator enumerator)
|
||||
{
|
||||
return mAsyncKitUnityBehaviour.StartCoroutine(enumerator);
|
||||
}
|
||||
|
||||
public static void StopCoroutine(Coroutine enumerator)
|
||||
{
|
||||
mAsyncKitUnityBehaviour.StopCoroutine(enumerator);
|
||||
}
|
||||
|
||||
public static void StartAction(string key, AsyncDealData.DealMethod method, float delayTime, int repeat = 1)
|
||||
{
|
||||
var temp = new AsyncDealData { dealMethod = method, DelayTime = delayTime, RepeatCount = repeat };
|
||||
mAsyncKitUnityBehaviour.AddAsyncData(key, temp);
|
||||
}
|
||||
|
||||
|
||||
public static void StopAction(string key)
|
||||
{
|
||||
mAsyncKitUnityBehaviour.RemoveAsyncData(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93122ca4429e463aba586a345558ed8e
|
||||
timeCreated: 1682577036
|
||||
Reference in New Issue
Block a user