/******************************************************************** 文件: SingletonAutoMono.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/03/29 17:28:19 最后修改: 梦语 最后修改时间: 2024/04/03 16:57:37 功能: Mono自动挂载单例 *********************************************************************/ using UnityEngine; public abstract class SingletonAutoMono : 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(); //#if !UNITY_EDITOR //DontDestroyOnLoad(obj); //#endif } return m_instance; } } private void Awake() { Debug.Log("初始化" + this.GetType().Name + "成功!"); } /// /// 初始化 /// public virtual void Init(){} /// /// 清除数据 /// public virtual void Clear(){} }