初始化
This commit is contained in:
16
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/IManager.cs
vendored
Normal file
16
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/IManager.cs
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace YIUIFramework
|
||||
{
|
||||
public interface IManager : ISingleton
|
||||
{
|
||||
/// <summary>
|
||||
/// 已经初始化结束 且成功了
|
||||
/// 失败了可以重复初始化
|
||||
/// </summary>
|
||||
bool InitedSucceed { get; }
|
||||
|
||||
//激活状态
|
||||
//激活被关闭时 Update,LateUpdate,FixedUpdate 都会被停止
|
||||
//其他不影响
|
||||
bool Enabled { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82c6da4a85b44ce693b1eb778dd072c4
|
||||
timeCreated: 1688978472
|
||||
@@ -0,0 +1,24 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
public interface IManagerAsyncInit : IManager
|
||||
{
|
||||
UniTask<bool> ManagerAsyncInit();
|
||||
}
|
||||
|
||||
public interface IManagerUpdate : IManager
|
||||
{
|
||||
void ManagerUpdate();
|
||||
}
|
||||
|
||||
public interface IManagerLateUpdate : IManager
|
||||
{
|
||||
void ManagerLateUpdate();
|
||||
}
|
||||
|
||||
public interface IManagerFixedUpdate : IManager
|
||||
{
|
||||
void ManagerFixedUpdate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bdd8efcb92b84e1f8a4e4775a6db53af
|
||||
timeCreated: 1688979733
|
||||
81
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/MgrCenter.cs
vendored
Normal file
81
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/MgrCenter.cs
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
//------------------------------------------------------------
|
||||
// Author: 亦亦
|
||||
// Mail: 379338943@qq.com
|
||||
// Data: 2023年2月12日
|
||||
//------------------------------------------------------------
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
//管理所有继承IManager的管理器
|
||||
public partial class MgrCenter : MonoSingleton<MgrCenter>
|
||||
{
|
||||
private MgrCore g_MgrCore = new MgrCore();
|
||||
|
||||
//失败的初始化队列 //可根据需求对失败的进行二次处理
|
||||
private Queue<IManager> m_FailInited = new Queue<IManager>();
|
||||
|
||||
//异步注册需要管理的 管理器 如果有异步初始化方法则会调用
|
||||
//返回初始化结果 如果失败是无法加入到管理中的
|
||||
//失败的可以重复调用直到成功
|
||||
//只有注册没有对应的移除 需要移除直接Dispose释放即可
|
||||
public async UniTask<bool> Register(IManager manager)
|
||||
{
|
||||
var result = await g_MgrCore.Add(manager);
|
||||
if (!result)
|
||||
{
|
||||
m_FailInited.Enqueue(manager);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//获取失败的管理器
|
||||
//队列出队 可能没有
|
||||
public IManager GetFailInitedMgr()
|
||||
{
|
||||
return m_FailInited.Dequeue();
|
||||
}
|
||||
|
||||
//获取当前失败数量
|
||||
public int GetFailInitedCount()
|
||||
{
|
||||
return m_FailInited.Count;
|
||||
}
|
||||
|
||||
//获取当前剩余的所有
|
||||
public List<IManager> GetFailInitedMgrList()
|
||||
{
|
||||
var list = new List<IManager>();
|
||||
var count = GetFailInitedCount();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
list.Add(GetFailInitedMgr());
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
g_MgrCore?.Update();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
g_MgrCore?.LateUpdate();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
g_MgrCore?.FixedUpdate();
|
||||
}
|
||||
|
||||
protected override void OnDispose()
|
||||
{
|
||||
g_MgrCore?.Dispose();
|
||||
g_MgrCore = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 017237d43f3145d2877980e4f337717c
|
||||
timeCreated: 1688978472
|
||||
211
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/MgrCenter_Core.cs
vendored
Normal file
211
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/MgrCenter_Core.cs
vendored
Normal file
@@ -0,0 +1,211 @@
|
||||
#define YIUIMACRO_SINGLETON_LOG
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
public partial class MgrCenter
|
||||
{
|
||||
//内部类 核心管理
|
||||
private class MgrCore
|
||||
{
|
||||
private List<IManager> m_MgrList = new List<IManager>();
|
||||
private List<IManagerUpdate> m_MgrUpdateList = new List<IManagerUpdate>();
|
||||
private List<IManagerLateUpdate> m_MgrLateUpdateList = new List<IManagerLateUpdate>();
|
||||
private List<IManagerFixedUpdate> m_MgrFixedUpdateList = new List<IManagerFixedUpdate>();
|
||||
private HashSet<IManager> m_CacheInitMgr = new HashSet<IManager>();
|
||||
|
||||
public async UniTask<bool> Add(IManager manager)
|
||||
{
|
||||
if (m_MgrList.Contains(manager))
|
||||
{
|
||||
Debug.LogError($"已存在Mgr {manager.GetType().Name} 请勿重复注册");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_CacheInitMgr.Contains(manager))
|
||||
{
|
||||
Debug.LogError($"{manager.GetType().Name} 请等待异步初始化中的管理器 请检查是否调用错误");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_CacheInitMgr.Add(manager);
|
||||
|
||||
if (manager is IManagerAsyncInit initialize)
|
||||
{
|
||||
#if YIUIMACRO_SINGLETON_LOG
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
#endif
|
||||
|
||||
var result = await initialize.ManagerAsyncInit();
|
||||
|
||||
#if YIUIMACRO_SINGLETON_LOG
|
||||
sw.Stop();
|
||||
Debug.Log($"<color=green>MgrCenter: 管理器[<color=Brown>{manager.GetType().Name}</color>]初始化耗时 {sw.ElapsedMilliseconds} 毫秒</color>");
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
{
|
||||
#if YIUIMACRO_SINGLETON_LOG
|
||||
Debug.Log($"<color=red>MgrCenter: 管理器[<color=Brown>{manager.GetType().Name}</color>]初始化失败</color>");
|
||||
#endif
|
||||
return false; //初始化失败的管理器 不添加
|
||||
}
|
||||
}
|
||||
|
||||
m_CacheInitMgr.Remove(manager);
|
||||
|
||||
#if YIUIMACRO_SINGLETON_LOG
|
||||
Debug.Log($"<color=navy>MgrCenter: 管理器[<color=Brown>{manager.GetType().Name}</color>]启动完毕</color>");
|
||||
#endif
|
||||
|
||||
m_MgrList.Add(manager);
|
||||
|
||||
if (manager is DisposerMonoSingleton)
|
||||
{
|
||||
//Mono单例无需检查UP 所以写了也不给你跑
|
||||
//MonoUP自行管理
|
||||
return true;
|
||||
}
|
||||
|
||||
if (manager is IManagerUpdate update)
|
||||
{
|
||||
m_MgrUpdateList.Add(update);
|
||||
}
|
||||
|
||||
if (manager is IManagerLateUpdate lateUpdate)
|
||||
{
|
||||
m_MgrLateUpdateList.Add(lateUpdate);
|
||||
}
|
||||
|
||||
if (manager is IManagerFixedUpdate fixedUpdate)
|
||||
{
|
||||
m_MgrFixedUpdateList.Add(fixedUpdate);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
for (int i = 0; i < m_MgrUpdateList.Count; i++)
|
||||
{
|
||||
IManagerUpdate manager = m_MgrUpdateList[i];
|
||||
|
||||
if (manager.Disposed) continue;
|
||||
if (!manager.Enabled) continue;
|
||||
|
||||
try
|
||||
{
|
||||
manager.ManagerUpdate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"管理器={manager.GetType().Name}, err={e.Message}{e.StackTrace}");
|
||||
}
|
||||
}
|
||||
|
||||
CheckDisposed();
|
||||
}
|
||||
|
||||
public void LateUpdate()
|
||||
{
|
||||
for (int i = 0; i < m_MgrLateUpdateList.Count; i++)
|
||||
{
|
||||
IManagerLateUpdate manager = m_MgrLateUpdateList[i];
|
||||
|
||||
if (manager.Disposed) continue;
|
||||
if (!manager.Enabled) continue;
|
||||
|
||||
try
|
||||
{
|
||||
manager.ManagerLateUpdate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"管理器={manager.GetType().Name}, err={e.Message}{e.StackTrace}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
for (int i = 0; i < m_MgrFixedUpdateList.Count; i++)
|
||||
{
|
||||
IManagerFixedUpdate manager = m_MgrFixedUpdateList[i];
|
||||
|
||||
if (manager.Disposed) continue;
|
||||
if (!manager.Enabled) continue;
|
||||
|
||||
try
|
||||
{
|
||||
manager.ManagerFixedUpdate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"管理器={manager.GetType().Name}, err={e.Message}{e.StackTrace}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckDisposed()
|
||||
{
|
||||
for (int i = m_MgrList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
IManager manager = m_MgrList[i];
|
||||
if (manager.Disposed)
|
||||
{
|
||||
Remove(manager);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Remove(IManager manager)
|
||||
{
|
||||
if (manager == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
#if YIUIMACRO_SINGLETON_LOG
|
||||
Debug.Log($"<color=navy>MgrCenter: 管理器[<color=Brown>{manager.GetType().Name}</color>]移除</color>");
|
||||
#endif
|
||||
|
||||
m_MgrList.Remove(manager);
|
||||
|
||||
if (manager is IManagerUpdate update)
|
||||
{
|
||||
m_MgrUpdateList.Remove(update);
|
||||
}
|
||||
|
||||
if (manager is IManagerLateUpdate lateUpdate)
|
||||
{
|
||||
m_MgrLateUpdateList.Remove(lateUpdate);
|
||||
}
|
||||
|
||||
if (manager is IManagerFixedUpdate fixedUpdate)
|
||||
{
|
||||
m_MgrFixedUpdateList.Remove(fixedUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
#if YIUIMACRO_SINGLETON_LOG
|
||||
Debug.Log("<color=navy>MgrCenter: 关闭MgrCenter</color>");
|
||||
#endif
|
||||
|
||||
//倒过来dispose
|
||||
for (int i = m_MgrList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
IManager manager = m_MgrList[i];
|
||||
manager.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ca700f0c372404f904acb583f98656d
|
||||
timeCreated: 1688979338
|
||||
55
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/MgrSingleton.cs
vendored
Normal file
55
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager/MgrSingleton.cs
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
//与Singleton 相比 mgr的单例可以受mgrcenter管理 可实现IManagerUpdate等操作
|
||||
public abstract class MgrSingleton<T> : Singleton<T>, IManagerAsyncInit where T : MgrSingleton<T>, new()
|
||||
{
|
||||
private bool m_Enabled;
|
||||
|
||||
public bool Enabled => m_Enabled;
|
||||
|
||||
private bool m_InitedSucceed;
|
||||
|
||||
public bool InitedSucceed => m_InitedSucceed;
|
||||
|
||||
public async UniTask<bool> ManagerAsyncInit()
|
||||
{
|
||||
if (m_InitedSucceed)
|
||||
{
|
||||
Debug.LogError($"{typeof(T).Name}已成功初始化过 请勿重复初始化");
|
||||
return true;
|
||||
}
|
||||
|
||||
var result = await MgrAsyncInit();
|
||||
if (!result)
|
||||
{
|
||||
Debug.LogError($"{typeof(T).Name} 初始化失败");
|
||||
}
|
||||
else
|
||||
{
|
||||
//成功初始化才记录
|
||||
m_InitedSucceed = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void SetEnabled(bool value)
|
||||
{
|
||||
m_Enabled = value;
|
||||
}
|
||||
|
||||
protected sealed override void OnInitSingleton()
|
||||
{
|
||||
//密封初始化方法 必须使用异步
|
||||
}
|
||||
|
||||
protected virtual async UniTask<bool> MgrAsyncInit()
|
||||
{
|
||||
await UniTask.CompletedTask;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 285a332b320a48078d565f1d7085eb4c
|
||||
timeCreated: 1689064532
|
||||
Reference in New Issue
Block a user