初始化
This commit is contained in:
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code.meta
vendored
Normal file
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code.meta
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9885625854a4f86b223ffde45b88634
|
||||
timeCreated: 1689064445
|
||||
6
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/ISingleton.cs
vendored
Normal file
6
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/ISingleton.cs
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace YIUIFramework
|
||||
{
|
||||
public interface ISingleton : IDisposer
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7dc01c9c44f340d888617bce800f46a4
|
||||
timeCreated: 1689062128
|
||||
87
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/SingletonMgr.cs
vendored
Normal file
87
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Code/SingletonMgr.cs
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// 所有继承了ISingleton的任意类型单例
|
||||
/// 主要目的为统计
|
||||
/// 不要轻易调用Dispose 一键释放所有单例
|
||||
/// </summary>
|
||||
public static class SingletonMgr
|
||||
{
|
||||
private static List<ISingleton> g_Singles = new List<ISingleton>();
|
||||
|
||||
public static bool Disposing { get; private set; } = true;
|
||||
|
||||
public static int Count => g_Singles.Count;
|
||||
|
||||
public static bool IsQuitting { get; private set; }
|
||||
|
||||
static SingletonMgr()
|
||||
{
|
||||
Application.quitting -= OnQuitting;
|
||||
Application.quitting += OnQuitting;
|
||||
}
|
||||
|
||||
private static void OnQuitting()
|
||||
{
|
||||
//Debug.LogError("OnQuitting");
|
||||
IsQuitting = true;
|
||||
}
|
||||
|
||||
//只能由一个地方 真的需要彻底清除时调用
|
||||
//游戏退出时不必调用 因为游戏都退了 所有都会被清空
|
||||
//需要调用的时机 如不退出游戏 但是要重置全部的情况下使用
|
||||
//一般情况是不需要使用的
|
||||
public static void Dispose()
|
||||
{
|
||||
if (IsQuitting)
|
||||
{
|
||||
//Debug.Log("正在退出游戏 不必清理");
|
||||
return;
|
||||
}
|
||||
|
||||
Disposing = true;
|
||||
|
||||
//Debug.Log($"SingletonMgr.清除所有单例");
|
||||
var singles = g_Singles.ToArray();
|
||||
for (int i = 0; i < singles.Length; i++)
|
||||
{
|
||||
var inst = singles[i];
|
||||
|
||||
if (inst == null || inst.Disposed) continue;
|
||||
|
||||
inst.Dispose();
|
||||
singles[i] = null;
|
||||
}
|
||||
|
||||
g_Singles.Clear();
|
||||
}
|
||||
|
||||
//初始化
|
||||
public static void Initialize()
|
||||
{
|
||||
if (IsQuitting)
|
||||
{
|
||||
Debug.Log("正在退出游戏 禁止初始化");
|
||||
return;
|
||||
}
|
||||
|
||||
//Debug.Log($"SingletonMgr.初始化");
|
||||
Disposing = false;
|
||||
}
|
||||
|
||||
internal static void Add(ISingleton single)
|
||||
{
|
||||
//Debug.Log($"添加{single.GetType().Name}");
|
||||
g_Singles.Add(single);
|
||||
}
|
||||
|
||||
internal static void Remove(ISingleton single)
|
||||
{
|
||||
//Debug.Log($"移除{single.GetType().Name}");
|
||||
g_Singles.Remove(single);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bb35a2b97b0444e93a55003422d212d
|
||||
timeCreated: 1688982499
|
||||
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager.meta
vendored
Normal file
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Manager.meta
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7624f0b553754c10938751361eed88b1
|
||||
timeCreated: 1688978472
|
||||
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
|
||||
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Singleton.meta
vendored
Normal file
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Singleton.meta
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f708c29021924685b2e8c81ca911cf12
|
||||
timeCreated: 1688982077
|
||||
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Singleton/Mono.meta
vendored
Normal file
3
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Singleton/Mono.meta
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30d6fc9b28e14b329ac5343201bc3f64
|
||||
timeCreated: 1689064415
|
||||
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
//基类使用odin 方便做一些显示
|
||||
//没有的不需要的可以改为UnityEngine.MonoBehaviour
|
||||
public abstract class DisposerMonoSingleton : SerializedMonoBehaviour,IManagerAsyncInit
|
||||
{
|
||||
private bool m_Disposed;
|
||||
public bool Disposed => m_Disposed;
|
||||
|
||||
//释放方法1: 对象释放
|
||||
public bool Dispose()
|
||||
{
|
||||
if (m_Disposed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_Disposed = true;
|
||||
OnDispose();
|
||||
gameObject.SafeDestroySelf();
|
||||
return true;
|
||||
}
|
||||
|
||||
//如非必要 任何子类都不要重写unity的OnDestroy
|
||||
//重写请base
|
||||
//推荐使用OnDispose
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
if (!m_Disposed)
|
||||
{
|
||||
if (GetDontDestroyOnLoad())
|
||||
{
|
||||
if (UIOperationHelper.IsPlaying())
|
||||
{
|
||||
//进入到这里说明不是被dispose 调用后摧毁的
|
||||
//而是直接被摧毁的 这种行为是不允许的
|
||||
Debug.LogError($"{this.name} 请调用 Dispose/DisposeInst 来移除Mono单例 而非直接删除GameObject对象");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否切换场景时不销毁销毁
|
||||
/// </summary>
|
||||
protected virtual bool GetDontDestroyOnLoad()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实例对象隐藏
|
||||
/// </summary>
|
||||
protected virtual bool GetHideAndDontSave()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理释放相关事情
|
||||
/// </summary>
|
||||
protected virtual void OnDispose()
|
||||
{
|
||||
}
|
||||
|
||||
//初始化回调
|
||||
protected virtual void OnInitSingleton()
|
||||
{
|
||||
}
|
||||
|
||||
//每次使用前回调
|
||||
protected virtual void OnUseSingleton()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public bool Enabled => true; //在mono中无效
|
||||
|
||||
private bool m_InitedSucceed;
|
||||
|
||||
public bool InitedSucceed => m_InitedSucceed;
|
||||
|
||||
public async UniTask<bool> ManagerAsyncInit()
|
||||
{
|
||||
if (m_InitedSucceed)
|
||||
{
|
||||
Debug.LogError($"{gameObject.name}已成功初始化过 请勿重复初始化");
|
||||
return true;
|
||||
}
|
||||
|
||||
var result = await MgrAsyncInit();
|
||||
if (!result)
|
||||
{
|
||||
Debug.LogError($"{gameObject.name} 初始化失败");
|
||||
}
|
||||
else
|
||||
{
|
||||
//成功初始化才记录
|
||||
m_InitedSucceed = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual async UniTask<bool> MgrAsyncInit()
|
||||
{
|
||||
await UniTask.CompletedTask;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30b6c37a9bc44eb39cdc4c5db4442f20
|
||||
timeCreated: 1688983106
|
||||
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// 组件的单例类基类
|
||||
/// 注意:这个单例实现适用于已经存在于场景的物件,但你需要使用Inst来访问。
|
||||
/// 它在场景上找不到时不会自动创建,如果有自动创建的需求,请使用MonoSingleton。
|
||||
/// 它也会不自动执行DontDestroyOnLoad, 这需要你自己在组件里写。
|
||||
/// </summary>
|
||||
public abstract class MonoSceneSingleton<T> : DisposerMonoSingleton where T : MonoSceneSingleton<T>
|
||||
{
|
||||
private static T g_Inst;
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在
|
||||
/// </summary>
|
||||
public static bool Exist => g_Inst != null;
|
||||
|
||||
/// <summary>
|
||||
/// 得到单例
|
||||
/// </summary>
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (g_Inst == null)
|
||||
{
|
||||
if (!UIOperationHelper.IsPlaying())
|
||||
{
|
||||
Debug.LogError($"非运行时 禁止调用");
|
||||
return null;
|
||||
}
|
||||
|
||||
Debug.LogError($" {typeof(T).Name} g_inst == null 这个是MonoSceneSingleton 需要自己创建对象的单例 不会自动创建");
|
||||
return null;
|
||||
}
|
||||
|
||||
g_Inst.OnUseSingleton();
|
||||
return g_Inst;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (SingletonMgr.Disposing)
|
||||
{
|
||||
Debug.LogError($" {typeof(T).Name} 单例管理器已释放或未初始化 禁止使用");
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_Inst != null)
|
||||
{
|
||||
Debug.LogError(typeof(T).Name + "是单例组件,不能在场景中存在多个");
|
||||
gameObject.SafeDestroySelf();
|
||||
return;
|
||||
}
|
||||
|
||||
g_Inst = (T)this;
|
||||
gameObject.name = g_Inst.GetCreateName();
|
||||
if (g_Inst.GetDontDestroyOnLoad())
|
||||
{
|
||||
DontDestroyOnLoad(g_Inst);
|
||||
}
|
||||
|
||||
if (g_Inst.GetHideAndDontSave())
|
||||
{
|
||||
gameObject.hideFlags = HideFlags.HideAndDontSave;
|
||||
}
|
||||
|
||||
SingletonMgr.Add(g_Inst);
|
||||
g_Inst.OnInitSingleton();
|
||||
}
|
||||
|
||||
private string GetCreateName()
|
||||
{
|
||||
return $"[Singleton]{typeof(T).Name}";
|
||||
}
|
||||
|
||||
//子类如果使用这个生命周期记得调用base
|
||||
//推荐使用 重写 OnDispose
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
SingletonMgr.Remove(g_Inst);
|
||||
g_Inst = null;
|
||||
}
|
||||
|
||||
//释放方法2: 静态释放
|
||||
public static bool DisposeInst()
|
||||
{
|
||||
if (g_Inst == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return g_Inst.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d49eb8b26fa4f5c80abc07eee00d443
|
||||
timeCreated: 1688985593
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// 组件的单例类基类
|
||||
/// 注意:如果这个单例实现不存在于场景,那么会自动在场景上创建一个GO,并把类挂在他下面
|
||||
/// 如果不希望自动创建,请使用MonoSceneSingleton。
|
||||
/// 它默认会设置DontDestroyOnLoad, 如果有其它需求,请覆写CanDestroyOnLoad。
|
||||
/// 它默认会给go一个合适的名字,如果有其它需求,请覆写CreateName。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class MonoSingleton<T> : DisposerMonoSingleton where T : MonoSingleton<T>
|
||||
{
|
||||
private static T g_Inst;
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在
|
||||
/// </summary>
|
||||
public static bool Exist => g_Inst != null;
|
||||
|
||||
/// <summary>
|
||||
/// 得到单例
|
||||
/// </summary>
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (g_Inst == null)
|
||||
{
|
||||
if (!UIOperationHelper.IsPlaying())
|
||||
{
|
||||
Debug.LogError($"非运行时 禁止调用");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (SingletonMgr.Disposing)
|
||||
{
|
||||
Debug.LogError($" {typeof (T).Name} 单例管理器已释放或未初始化 禁止使用");
|
||||
return null;
|
||||
}
|
||||
|
||||
GameObject go = new GameObject();
|
||||
g_Inst = go.AddComponent<T>();
|
||||
go.name = g_Inst.GetCreateName();
|
||||
if (g_Inst.GetDontDestroyOnLoad())
|
||||
{
|
||||
DontDestroyOnLoad(go);
|
||||
}
|
||||
|
||||
if (g_Inst.GetHideAndDontSave())
|
||||
{
|
||||
go.hideFlags = HideFlags.HideAndDontSave;
|
||||
}
|
||||
|
||||
SingletonMgr.Add(g_Inst);
|
||||
g_Inst.OnInitSingleton();
|
||||
}
|
||||
|
||||
g_Inst.OnUseSingleton();
|
||||
return g_Inst;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetCreateName()
|
||||
{
|
||||
return $"[Singleton]{typeof(T).Name}";
|
||||
}
|
||||
|
||||
protected override bool GetHideAndDontSave()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//子类如果使用这个生命周期记得调用base
|
||||
//推荐使用 重写 OnDispose
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
SingletonMgr.Remove(g_Inst);
|
||||
g_Inst = null;
|
||||
}
|
||||
|
||||
//释放方法2: 静态释放
|
||||
public static bool DisposeInst()
|
||||
{
|
||||
if (g_Inst == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return g_Inst.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c5958e3684e49ec910b8d0f6b894eaa
|
||||
timeCreated: 1688977933
|
||||
97
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Singleton/Singleton.cs
vendored
Normal file
97
UnityGame/Assets/Scripts/ThirdParty/YIUIFramework/Plugins/YIUISingleton/Singleton/Singleton.cs
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YIUIFramework
|
||||
{
|
||||
public abstract class Singleton<T> : ISingleton where T : Singleton<T>, new()
|
||||
{
|
||||
private static T g_Inst;
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在
|
||||
/// </summary>
|
||||
public static bool Exist => g_Inst != null;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已释放
|
||||
/// </summary>
|
||||
private bool m_Disposed;
|
||||
|
||||
public bool Disposed => m_Disposed;
|
||||
|
||||
protected Singleton()
|
||||
{
|
||||
if (g_Inst != null)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
throw new Exception(this + "是单例类,不能实例化两次");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (g_Inst == null)
|
||||
{
|
||||
if (SingletonMgr.Disposing)
|
||||
{
|
||||
Debug.LogError($" {typeof (T).Name} 单例管理器已释放或未初始化 禁止使用");
|
||||
return null;
|
||||
}
|
||||
|
||||
g_Inst = new T();
|
||||
g_Inst.OnInitSingleton();
|
||||
SingletonMgr.Add(g_Inst);
|
||||
}
|
||||
|
||||
g_Inst.OnUseSingleton();
|
||||
return g_Inst;
|
||||
}
|
||||
}
|
||||
|
||||
//释放方法2: 静态释放
|
||||
public static bool DisposeInst()
|
||||
{
|
||||
if (g_Inst == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return g_Inst.Dispose();
|
||||
}
|
||||
|
||||
//释放方法1: 对象释放
|
||||
public bool Dispose()
|
||||
{
|
||||
if (m_Disposed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
SingletonMgr.Remove(g_Inst);
|
||||
g_Inst = default;
|
||||
m_Disposed = true;
|
||||
OnDispose();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理释放相关事情
|
||||
/// </summary>
|
||||
protected virtual void OnDispose()
|
||||
{
|
||||
}
|
||||
|
||||
//初始化回调
|
||||
protected virtual void OnInitSingleton()
|
||||
{
|
||||
}
|
||||
|
||||
//每次使用前回调
|
||||
protected virtual void OnUseSingleton()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a70217af1d184df6804d8a4f3b3968ee
|
||||
timeCreated: 1681720464
|
||||
Reference in New Issue
Block a user