IndieGame/client/Assets/Ether/Scripts/Common/Singleton/SingletonAutoMono.cs
DOBEST\zhaoyingjie f242607587 初始化工程
2024-10-11 10:12:15 +08:00

58 lines
1.5 KiB
C#

/********************************************************************
文件: SingletonAutoMono.cs
作者: 梦语
邮箱: 1982614048@qq.com
创建时间: 2024/03/29 17:28:19
最后修改: 梦语
最后修改时间: 2024/04/03 16:57:37
功能: Mono自动挂载单例
*********************************************************************/
using UnityEngine;
public abstract class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static T m_instance;
public static T Inst
{
get
{
if (m_instance == null)
{
GameObject mgrRoot = GameObject.FindGameObjectWithTag("ManagerRoot");
if (mgrRoot == null)
{
mgrRoot = new GameObject("ManagerRoot");
mgrRoot.tag = "ManagerRoot";
DontDestroyOnLoad(mgrRoot);
}
//设置对象名为脚本名
GameObject obj = new GameObject(typeof(T).Name);
obj.transform.SetParent(mgrRoot.transform);
m_instance = obj.AddComponent<T>();
//#if !UNITY_EDITOR
//DontDestroyOnLoad(obj);
//#endif
}
return m_instance;
}
}
private void Awake()
{
Debug.Log("初始化" + this.GetType().Name + "成功!");
}
/// <summary>
/// 初始化
/// </summary>
public virtual void Init(){}
/// <summary>
/// 清除数据
/// </summary>
public virtual void Clear(){}
}