using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
|
public class LogicEngine : MonoBehaviour
|
{
|
public static LogicEngine Instance { get; private set; }
|
|
[RuntimeInitializeOnLoadMethod]
|
static void Init()
|
{
|
if (FindObjectOfType<LogicEngine>() == null)
|
{
|
var gameObject = new GameObject("LogicEngine");
|
Instance = gameObject.AddMissingComponent<LogicEngine>();
|
GameObject.DontDestroyOnLoad(gameObject);
|
}
|
}
|
|
List<LogicUpdate> logicUpdates = new List<LogicUpdate>();
|
public void Register(LogicUpdate logicUpdate)
|
{
|
if (!logicUpdates.Contains(logicUpdate))
|
{
|
logicUpdates.Add(logicUpdate);
|
}
|
}
|
|
public void UnRegister(LogicUpdate logicUpdate)
|
{
|
if (logicUpdates.Contains(logicUpdate))
|
{
|
logicUpdates.Remove(logicUpdate);
|
}
|
}
|
|
void Update()
|
{
|
for (var i = logicUpdates.Count - 1; i >= 0; i--)
|
{
|
var item = logicUpdates[i];
|
if (item.destroyDirty)
|
{
|
logicUpdates.RemoveAt(i);
|
}
|
else
|
{
|
try
|
{
|
item.Update();
|
}
|
catch (System.Exception ex)
|
{
|
DebugEx.LogError(ex);
|
}
|
}
|
}
|
|
}
|
|
}
|