2023-04-03 11:04:31 +08:00

68 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace BF
{
public class OneShotManager : ManagerBase
{
static OneShotManager instance;
public static OneShotManager Create()
{
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
instance = new OneShotManager();
return instance;
}
List<Action> oneShotList;
int updateFrame = -1;
int executeCount;
OneShotManager() { }
public override void Init()
{
oneShotList = new List<Action>(16);
}
public override void Destroy()
{
oneShotList.Clear();
instance = null;
}
public override void Update()
{
updateFrame = Time.frameCount;
int totalCount = oneShotList.Count;
if (totalCount > 0)
{
int count = executeCount;
executeCount = totalCount - count;
if (executeCount < 0)
{
executeCount = 0;
}
if (count > 0)
{
for (int i = 0; i < count; i++)
{
oneShotList[i]();
}
oneShotList.RemoveRange(0, count);
}
}
}
public void AddOneShot(Action oneShot)
{
if (Time.frameCount == updateFrame)
{
executeCount++;
}
oneShotList.Add(oneShot);
}
}
}