using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Templete
{
///
/// 布尔检查器
///
public class CheckTick : MonoBehaviour
{
private static readonly object lockObj = new object();
private static CheckTick __;
private static CheckTick _
{
get
{
if(__ == null)
{
lock (lockObj)
{
if(__ == null)
{
__ = FindObjectOfType();
if(__ == null)
{
__ = new GameObject("CheckTick").AddComponent();
}
}
}
}
return __;
}
}
private void Awake()
{
lock (lockObj)
{
CheckTick[] monos = FindObjectsOfType();
if (monos.Length > 1)
{
#if UNITY_EDITOR
if (Application.isPlaying)
{
Destroy(this);
}
else
{
DestroyImmediate(this);
}
#else
Destroy(this);
#endif
}
else
{
if (Application.isPlaying)
{
DontDestroyOnLoad(gameObject);
}
}
}
}
private readonly LinkedList functions = new LinkedList();
[SerializeField]
private float interval = 0.033f;
private float currInterval = 0f;
private bool AfterInterval()
{
if (Time.unscaledTime - currInterval > interval)
{
currInterval = 0;
return true;
}
return false;
}
///
/// 更新间隔
///
public static float Interval
{
get
{
return _.interval;
}
set
{
if(value < 0)
{
value = 0;
}
_.interval = value;
}
}
///
/// 添加规则
///
///
public static void AddRule(CheckTickFunctions.CheckTickFunc condition, CheckTickFunctions.CheckTickFunc todo)
{
CheckTickFunctions check = new CheckTickFunctions(condition, todo);
if (check.Initialized)
{
_.functions.AddLast(check);
}
}
private void Tick()
{
if ( functions.Count > 0)
{
LinkedListNode node = functions.First;
while (node != null)
{
CheckTickFunctions check = node.Value;
if (check.condition())
{
if (check.todo())
{
LinkedListNode del = node;
node = node.Next;
functions.Remove(del);
continue;
}
}
node = node.Next;
}
}
}
public enum UpdateMethodEnum
{
Update, LateUpdate
}
[SerializeField]
private UpdateMethodEnum updateMethod;
///
/// 更新方法
///
public static UpdateMethodEnum UpdateMethod
{
get
{
return _.updateMethod;
}
set
{
_.updateMethod = value;
}
}
private void Update()
{
if(updateMethod == UpdateMethodEnum.Update)
{
Tick();
}
}
private void LateUpdate()
{
if (updateMethod == UpdateMethodEnum.LateUpdate)
{
Tick();
}
}
}
///
/// 检查器规则函数
///
public class CheckTickFunctions
{
public CheckTickFunctions(CheckTickFunc condition, CheckTickFunc todo)
{
if(condition != null && todo != null)
{
this.condition = condition;
this.todo = todo;
}
else
{
Debug.LogError("CheckTickFunctions(Func condition, Func todo) 参数不能为空");
}
}
///
/// 是否已经初始化
///
public bool Initialized
{
get
{
if(condition != null && todo != null)
{
return true;
}
return false;
}
}
public delegate bool CheckTickFunc();
///
/// 条件,返回true则执行todo,返回false继续下一次判断
///
public CheckTickFunc condition;
///
/// 执行,返回true则结束,返回false继续下一次条件判断
///
public CheckTickFunc todo;
}
}