初始化
This commit is contained in:
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