179 lines
4.6 KiB
C#
179 lines
4.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
namespace SLSUtilities.General
|
|
{
|
|
public class PrioritizedFunctionBase : IPrioritized
|
|
{
|
|
public int Priority { get; set; }
|
|
|
|
/// <summary>
|
|
/// 执行计数。
|
|
/// -1 代表无限次执行。
|
|
/// >0 代表剩余执行次数,每次 Invoke 后递减,减至 0 时应被移除。
|
|
/// </summary>
|
|
public int ExecutionCount { get; set; } = -1;
|
|
|
|
public bool ShouldRemove => ExecutionCount == 0;
|
|
|
|
protected void UpdateCount()
|
|
{
|
|
if (ExecutionCount > 0) ExecutionCount--;
|
|
}
|
|
}
|
|
|
|
#region PrioritizedAction
|
|
public class PrioritizedAction : PrioritizedFunctionBase
|
|
{
|
|
private readonly Action action;
|
|
|
|
public PrioritizedAction(Action action, int priority = 0, int count = -1)
|
|
{
|
|
this.action = action;
|
|
Priority = priority;
|
|
ExecutionCount = count;
|
|
}
|
|
|
|
public void Invoke()
|
|
{
|
|
action.Invoke();
|
|
UpdateCount();
|
|
}
|
|
}
|
|
|
|
public class PrioritizedAction<T> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Action<T> action;
|
|
|
|
public PrioritizedAction(Action<T> action, int priority = 0, int count = -1)
|
|
{
|
|
this.action = action;
|
|
Priority = priority;
|
|
ExecutionCount = count;
|
|
}
|
|
|
|
public void Invoke(T arg)
|
|
{
|
|
action.Invoke(arg);
|
|
UpdateCount();
|
|
}
|
|
}
|
|
|
|
public class PrioritizedAction<T0, T1> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Action<T0, T1> action;
|
|
|
|
public PrioritizedAction(Action<T0, T1> action, int priority = 0, int count = -1)
|
|
{
|
|
this.action = action;
|
|
Priority = priority;
|
|
ExecutionCount = count;
|
|
}
|
|
|
|
public void Invoke(T0 arg0, T1 arg1)
|
|
{
|
|
action.Invoke(arg0, arg1);
|
|
UpdateCount();
|
|
}
|
|
}
|
|
|
|
public class PrioritizedAction<T0, T1, T2> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Action<T0, T1, T2> action;
|
|
|
|
public PrioritizedAction(Action<T0, T1, T2> action, int priority = 0, int count = -1)
|
|
{
|
|
this.action = action;
|
|
Priority = priority;
|
|
ExecutionCount = count;
|
|
}
|
|
|
|
public void Invoke(T0 arg0, T1 arg1, T2 arg2)
|
|
{
|
|
action.Invoke(arg0, arg1, arg2);
|
|
UpdateCount();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region PrioritizedFunc
|
|
|
|
public class PrioritizedFunc<TR> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Func<TR> func;
|
|
|
|
public PrioritizedFunc(Func<TR> func, int priority = 0, int count = -1)
|
|
{
|
|
this.func = func;
|
|
this.Priority = priority;
|
|
this.ExecutionCount = count;
|
|
}
|
|
|
|
public TR Invoke()
|
|
{
|
|
TR result = func.Invoke();
|
|
UpdateCount();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public class PrioritizedFunc<T, TR> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Func<T, TR> func;
|
|
|
|
public PrioritizedFunc(Func<T, TR> func, int priority = 0, int count = -1)
|
|
{
|
|
this.func = func;
|
|
this.Priority = priority;
|
|
this.ExecutionCount = count;
|
|
}
|
|
|
|
public TR Invoke(T arg)
|
|
{
|
|
TR result = func.Invoke(arg);
|
|
UpdateCount();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public class PrioritizedFunc<T0, T1, TR> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Func<T0, T1, TR> func;
|
|
|
|
public PrioritizedFunc(Func<T0, T1, TR> func, int priority = 0, int count = -1)
|
|
{
|
|
this.func = func;
|
|
this.Priority = priority;
|
|
this.ExecutionCount = count;
|
|
}
|
|
|
|
public TR Invoke(T0 arg0, T1 arg1)
|
|
{
|
|
TR result = func.Invoke(arg0, arg1);
|
|
UpdateCount();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public class PrioritizedFunc<T0, T1, T2, TR> : PrioritizedFunctionBase
|
|
{
|
|
private readonly Func<T0, T1, T2, TR> func;
|
|
|
|
public PrioritizedFunc(Func<T0, T1, T2, TR> func, int priority = 0, int count = -1)
|
|
{
|
|
this.func = func;
|
|
this.Priority = priority;
|
|
this.ExecutionCount = count;
|
|
}
|
|
|
|
public TR Invoke(T0 arg0, T1 arg1, T2 arg2)
|
|
{
|
|
TR result = func.Invoke(arg0, arg1, arg2);
|
|
UpdateCount();
|
|
return result;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|