调整了单例类的功能

This commit is contained in:
2023-10-26 15:24:54 +08:00
parent 498e56bb69
commit dc96b23354
2 changed files with 26 additions and 13 deletions
+19 -6
View File
@@ -6,14 +6,20 @@ using UnityEngine;
namespace XericLibrary.Runtime.Type
{
/// <summary>
/// 单例基础类,
/// 单例基础类,只能基于MonoBehaviour类进行单例扩展;
/// 指定任何其他的泛型类都会丢失其中的呃
/// </summary>
/// <typeparam name="T"></typeparam>
public class SingleMonoBase<T> : MonoBehaviour
public class SingleMonoBase<T> : MonoBehaviour
where T : Component
{
#region 字段属性
/// <summary>
/// 根节点名称
/// </summary>
private static string rootNodeName = "DoNotDestroyThis";
/// <summary>
/// 组件单例
/// </summary>
@@ -52,7 +58,7 @@ namespace XericLibrary.Runtime.Type
inst = this as T;
if(inst == null)
throw new System.Exception("单例脚本给定类型错误");
throw new System.Exception("单例类型错误:单例系统不可以扩展一个并非自己的类");
}
#endregion
@@ -60,7 +66,7 @@ namespace XericLibrary.Runtime.Type
#region 方法
/// <summary>
/// 移除一个单例实例
/// 移除此处的单例实例
/// </summary>
protected bool DestroyInstance()
{
@@ -77,8 +83,15 @@ namespace XericLibrary.Runtime.Type
/// </summary>
private static void InstanceLaunch()
{
if (globalEmptyNode == null)
globalEmptyNode = new GameObject("DoNotDestroyThis");
if(globalEmptyNode == null)
{
#if UNITY_EDITOR
globalEmptyNode = GameObject.Find(rootNodeName);
if(globalEmptyNode == null)
#endif
globalEmptyNode = new GameObject(rootNodeName);
}
globalEmptyNode.AddComponent<T>();
}