namespace TEngine.Runtime
{
/// <summary>
/// 逻辑生命周期系统接口
/// </summary>
public interface ILogicSys
{
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
bool OnInit();
/// <summary>
/// 销毁
/// </summary>
void OnDestroy();
/// <summary>
/// 开始
/// </summary>
void OnStart();
/// <summary>
/// 一帧中立刻执行
/// </summary>
void OnUpdate();
/// <summary>
/// 一帧最后执行
/// </summary>
void OnLateUpdate();
/// <summary>
/// 暂停时
/// </summary>
void OnPause();
/// <summary>
/// 重新唤醒时
/// </summary>
void OnResume();
/// <summary>
/// 绘制
/// </summary>
void OnDrawGizmos();
}
}
上面是关于系统的逻辑生命周期的接口封装
namespace TEngine.Runtime
{
/// <summary>
/// 基础LogicSys,生命周期由TEngine实现,推荐给系统实现,
/// 减少多余的Mono,保持系统层面只有一个Update
/// 用TEngine的主Mono来驱动LogicSys的生命周期
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseLogicSys<T> : ILogicSys where T : new()
{
private static T m_Instance;
/// <summary>
/// 是否有逻辑生命周期单例对象
/// </summary>
public static bool HasInstance
{
get { return m_Instance != null; }
}
public static T Instance
{
get
{
if (null == m_Instance)
{
m_Instance = new T();
}
return m_Instance;
}
}
#region virtual function
public virtual bool OnInit()
{
if (null == m_Instance)
{
m_Instance = new T();
}
return true;
}
public virtual void OnStart()
{
}
public virtual void OnUpdate()
{
}
public virtual void OnLateUpdate()
{
}
public virtual void OnDestroy()
{
}
public virtual void OnPause()
{
}
public virtual void OnResume()
{
}
public virtual void OnDrawGizmos()
{
}
public virtual void OnMapChanged()
{
}
#endregion
}
}
上面是对系统的逻辑生命周期接口的是现实。
using System;
using System.Collections.Generic;
using UnityEngine;
namespace TEngine.Runtime
{
public partial class TEngineCore : UnitySingleton<TEngineCore>
{
private const int DefaultDpi = 96;
protected override void OnLoad()
{
InitLibImp();
RegisterAllSystem();
Utility.Converter.ScreenDpi = Screen.dpi;
if (Utility.Converter.ScreenDpi <= 0)
{
Utility.Converter.ScreenDpi = DefaultDpi;
}
}
/// <summary>
/// 注册实现库
/// </summary>
protected virtual void InitLibImp()
{
}
/// <summary>
/// 注册系统(例如BaseLogic/TEngineObject/MonoManger)
/// </summary>
protected virtual void RegisterAllSystem()
{
}
protected void SetTargetFrameRate(int targetFrameRate)
{
Application.targetFrameRate = targetFrameRate;
}
//-------------------------------------------------------系统注册--------------------------------------------------------//
private List<ILogicSys> m_LogicMgrList = new List<ILogicSys>();
/// <summary>
/// 检查是否有该系统
/// </summary>
/// <param name="logicSys"></param>
/// <returns></returns>
public bool ContainLogicSys(ILogicSys logicSys)
{
return m_LogicMgrList.Contains(logicSys);
}
/// <summary>
/// 系统注册
/// </summary>
/// <param name="logicSys"></param>
/// <returns></returns>
public bool AddLogicSys(ILogicSys logicSys)
{
if (m_LogicMgrList.Contains(logicSys))
{
TLogger.LogInfo("Repeat add logic system: " + logicSys.GetType().Name);
return false;
}
if (!logicSys.OnInit())
{
TLogger.LogInfo(" Init failed " + logicSys.GetType().Name);
return false;
}
m_LogicMgrList.Add(logicSys);
return true;
}
/// <summary>
/// 系统注销
/// </summary>
/// <param name="logicSys"></param>
/// <returns></returns>
public void RemoveLogicSys(ILogicSys logicSys)
{
if (m_LogicMgrList.Contains(logicSys))
{
TLogger.LogInfo("Remove logic system: " + logicSys.GetType().Name);
logicSys.OnDestroy();
m_LogicMgrList.Remove(logicSys);
logicSys = null;
}
}
#region 生命周期
public void Start()
{
var listLogic = m_LogicMgrList;
var logicCnt = listLogic.Count;
for (int i = 0; i < logicCnt; i++)
{
var logic = listLogic[i];
logic.OnStart();
}
}
public void Update()
{
var listLogic = m_LogicMgrList;
var logicCnt = listLogic.Count;
for (int i = 0; i < logicCnt; i++)
{
var logic = listLogic[i];
logic.OnUpdate();
}
}
public void LateUpdate()
{
var listLogic = m_LogicMgrList;
var logicCnt = listLogic.Count;
for (int i = 0; i < logicCnt; i++)
{
var logic = listLogic[i];
logic.OnLateUpdate();
}
}
public void OnPause()
{
for (int i = 0; i < m_LogicMgrList.Count; i++)
{
var logicSys = m_LogicMgrList[i];
logicSys.OnPause();
}
}
public void OnResume()
{
for (int i = 0; i < m_LogicMgrList.Count; i++)
{
var logicSys = m_LogicMgrList[i];
logicSys.OnResume();
}
}
public override void OnDestroy()
{
for (int i = 0; i < m_LogicMgrList.Count; i++)
{
var logicSys = m_LogicMgrList[i];
logicSys.OnDestroy();
}
base.OnDestroy();
}
public void OnDrawGizmos()
{
for (int i = 0; i < m_LogicMgrList.Count; i++)
{
var logicSys = m_LogicMgrList[i];
logicSys.OnDrawGizmos();
}
}
#endregion
}
}
上面是对所有系统的注册、注销以及系统各生命周期阶段的接口的实现。
下面是具体的使用。对于具备Unity完整生命周期的MonoBehaviuor组件的封装。自定义的一些直接需要直接继承自MonoBehaviour组件的脚本,可以通过BehaviourSingleton达到同样的效果,而且避免多个MonoBehaviour之间控制问顺序题,各自模块的Update顺序由各模块定义的优先级决定,这样能做到执行顺序可控。
/// <summary>
/// Behaviour单例基类
/// </summary>
public class BaseBehaviourSingleton
{
public bool IsStart = false;
/// <summary>
/// 激活
/// </summary>
public virtual void Active()
{
}
/// <summary>
/// 创建时
/// </summary>
public virtual void Awake()
{
}
public virtual bool IsHaveLateUpdate()
{
return false;
}
/// <summary>
/// 开始时
/// </summary>
public virtual void Start()
{
}
/// <summary>
/// 每一帧立刻更新
/// </summary>
public virtual void Update()
{
}
/// <summary>
/// ,每一帧最后更新
/// </summary>
public virtual void LateUpdate()
{
}
/// <summary>
/// 销毁
/// </summary>
public virtual void Destroy()
{
}
/// <summary>
/// 暂停
/// </summary>
public virtual void OnPause()
{
}
/// <summary>
/// 唤醒时
/// </summary>
public virtual void OnResume()
{
}
/// <summary>
/// 绘制时
/// </summary>
public virtual void OnDrawGizmos()
{
}
}
/// <summary>
/// 通过LogicSys来驱动且具备Unity完整生命周期的单例(不继承MonoBehaviour)
/// </summary>
/// <typeparam name="T"></typeparam>
public class BehaviourSingleton<T> : BaseBehaviourSingleton where T : BaseBehaviourSingleton, new()
{
private static T sInstance;
/// <summary>
/// Behaviour单例
/// </summary>
public static T Instance
{
get
{
if (null == sInstance)
{
sInstance = new T();
BehaviourSingleSystem.Instance.CheckInstance();
TLogger.LogAssert(sInstance != null);
sInstance.Awake();
RegSingleton(sInstance);
}
return sInstance;
}
}
/// <summary>
/// Behaviour单例注册
/// </summary>
/// <param name="inst"></param>
private static void RegSingleton(BaseBehaviourSingleton inst)
{
BehaviourSingleSystem.Instance.RegSingleton(inst);
}
}
上面是对MonoBehaviour接口的封装
/// <summary>
/// Behaviour逻辑生命周期对象的管理控制系统
/// </summary>
public class BehaviourSingleSystem : BaseLogicSys<BehaviourSingleSystem>
{
/// <summary>
/// Behaviuor对象列表
/// </summary>
List<BaseBehaviourSingleton> m_listInst = new List<BaseBehaviourSingleton>();
/// <summary>
/// 已经初始化了的Behaviour对象列表
/// </summary>
List<BaseBehaviourSingleton> m_listStart = new List<BaseBehaviourSingleton>();
/// <summary>
/// 执行Update的Behaviour对象列表
/// </summary>
List<BaseBehaviourSingleton> m_listUpdate = new List<BaseBehaviourSingleton>();
/// <summary>
/// 执行LateUpdate的Behaviour对象列表
/// </summary>
List<BaseBehaviourSingleton> m_listLateUpdate = new List<BaseBehaviourSingleton>();
private bool _hadInstance = false;
/// <summary>
/// 检查是否有单例系统
/// </summary>
/// <returns></returns>
public bool CheckInstance()
{
if (!_hadInstance)
{
if (!TEngineCore.Instance.ContainLogicSys(BehaviourSingleSystem.Instance))
{
TEngineCore.Instance.AddLogicSys(BehaviourSingleSystem.Instance);
}
_hadInstance = true;
}
return _hadInstance;
}
/// <summary>
/// Behaviour组件注册
/// </summary>
/// <param name="inst"></param>
public void RegSingleton(BaseBehaviourSingleton inst)
{
TLogger.LogAssert(!m_listInst.Contains(inst));
m_listInst.Add(inst);
m_listStart.Add(inst);
}
/// <summary>
/// Behaviour组件注销
/// </summary>
/// <param name="inst"></param>
public void UnRegSingleton(BaseBehaviourSingleton inst)
{
if (inst == null)
{
TLogger.LogError($"BaseBehaviourSingleton Is Null");
return;
}
TLogger.LogAssert(m_listInst.Contains(inst));
if (m_listInst.Contains(inst))
{
m_listInst.Remove(inst);
}
if (m_listStart.Contains(inst))
{
m_listStart.Remove(inst);
}
if (m_listUpdate.Contains(inst))
{
m_listUpdate.Remove(inst);
}
if (m_listLateUpdate.Contains(inst))
{
m_listLateUpdate.Remove(inst);
}
inst.Destroy();
inst = null;
}
/// <summary>
/// 每帧刷新
/// </summary>
public override void OnUpdate()
{
var listStart = m_listStart;
var listToUpdate = m_listUpdate;
var listToLateUpdate = m_listLateUpdate;
if (listStart.Count > 0)
{
for (int i = 0; i < listStart.Count; i++)
{
var inst = listStart[i];
TLogger.LogAssert(!inst.IsStart);
inst.IsStart = true;
inst.Start();
listToUpdate.Add(inst);
if (inst.IsHaveLateUpdate())
{
listToLateUpdate.Add(inst);
}
}
listStart.Clear();
}
var listUpdateCnt = listToUpdate.Count;
for (int i = 0; i < listUpdateCnt; i++)
{
var inst = listToUpdate[i];
TProfiler.BeginFirstSample(inst.GetType().FullName);
inst.Update();
TProfiler.EndFirstSample();
}
}
/// <summary>
/// 每一帧的最后刷新
/// </summary>
public override void OnLateUpdate()
{
var listLateUpdate = m_listLateUpdate;
var listLateUpdateCnt = listLateUpdate.Count;
for (int i = 0; i < listLateUpdateCnt; i++)
{
var inst = listLateUpdate[i];
TProfiler.BeginFirstSample(inst.GetType().FullName);
inst.LateUpdate();
TProfiler.EndFirstSample();
}
}
/// <summary>
/// 销毁时
/// </summary>
public override void OnDestroy()
{
for (int i = 0; i < m_listInst.Count; i++)
{
var inst = m_listInst[i];
inst.Destroy();
}
}
/// <summary>
/// 暂停时
/// </summary>
public override void OnPause()
{
for (int i = 0; i < m_listInst.Count; i++)
{
var inst = m_listInst[i];
inst.OnPause();
}
}
/// <summary>
/// 唤醒时
/// </summary>
public override void OnResume()
{
for (int i = 0; i < m_listInst.Count; i++)
{
var inst = m_listInst[i];
inst.OnResume();
}
}
/// <summary>
/// 绘制时
/// </summary>
public override void OnDrawGizmos()
{
for (int i = 0; i < m_listInst.Count; i++)
{
var inst = m_listInst[i];
inst.OnDrawGizmos();
}
}
}
上面是对所有Behaviour单例对象的整体管理控制:注册、注销、以及各生命周期接口的统一调用。