IndieGame/client/Assets/Ether/Scripts/Common/Singleton/SingletonAutoMono.cs

58 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-10-11 10:12:15 +08:00
/********************************************************************
: 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(){}
}