TH1/Unity/Assets/Scripts/ThirdParty/ETTask/StateMachineWrap.cs
2025-07-17 18:26:28 +08:00

59 lines
1.3 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
namespace ET
{
public interface IStateMachineWrap
{
Action MoveNext { get; }
void Recycle();
}
public class StateMachineWrap<T>: IStateMachineWrap where T: IAsyncStateMachine
{
private static readonly ConcurrentQueue<StateMachineWrap<T>> queue = new();
public static StateMachineWrap<T> Fetch(ref T stateMachine)
{
if (!queue.TryDequeue(out var stateMachineWrap))
{
stateMachineWrap = new StateMachineWrap<T>();
}
stateMachineWrap.StateMachine = stateMachine;
return stateMachineWrap;
}
public void Recycle()
{
if (queue.Count > 100)
{
return;
}
this.StateMachine = default;
queue.Enqueue(this);
}
private readonly Action moveNext;
public Action MoveNext
{
get
{
return this.moveNext;
}
}
private T StateMachine;
private StateMachineWrap()
{
this.moveNext = this.Run;
}
private void Run()
{
this.StateMachine.MoveNext();
}
}
}