调整了单例类的功能

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
+17 -4
View File
@@ -6,7 +6,8 @@ using UnityEngine;
namespace XericLibrary.Runtime.Type namespace XericLibrary.Runtime.Type
{ {
/// <summary> /// <summary>
/// 单例基础类, /// 单例基础类,只能基于MonoBehaviour类进行单例扩展;
/// 指定任何其他的泛型类都会丢失其中的呃
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public class SingleMonoBase<T> : MonoBehaviour public class SingleMonoBase<T> : MonoBehaviour
@@ -14,6 +15,11 @@ namespace XericLibrary.Runtime.Type
{ {
#region 字段属性 #region 字段属性
/// <summary>
/// 根节点名称
/// </summary>
private static string rootNodeName = "DoNotDestroyThis";
/// <summary> /// <summary>
/// 组件单例 /// 组件单例
/// </summary> /// </summary>
@@ -52,7 +58,7 @@ namespace XericLibrary.Runtime.Type
inst = this as T; inst = this as T;
if(inst == null) if(inst == null)
throw new System.Exception("单例脚本给定类型错误"); throw new System.Exception("单例类型错误:单例系统不可以扩展一个并非自己的类");
} }
#endregion #endregion
@@ -60,7 +66,7 @@ namespace XericLibrary.Runtime.Type
#region 方法 #region 方法
/// <summary> /// <summary>
/// 移除一个单例实例 /// 移除此处的单例实例
/// </summary> /// </summary>
protected bool DestroyInstance() protected bool DestroyInstance()
{ {
@@ -78,7 +84,14 @@ namespace XericLibrary.Runtime.Type
private static void InstanceLaunch() private static void InstanceLaunch()
{ {
if(globalEmptyNode == null) if(globalEmptyNode == null)
globalEmptyNode = new GameObject("DoNotDestroyThis"); {
#if UNITY_EDITOR
globalEmptyNode = GameObject.Find(rootNodeName);
if(globalEmptyNode == null)
#endif
globalEmptyNode = new GameObject(rootNodeName);
}
globalEmptyNode.AddComponent<T>(); globalEmptyNode.AddComponent<T>();
} }