using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; namespace MuHua { /// /// 页面单例 /// public abstract class ModuleUISingle : MonoBehaviour where Single : ModuleUISingle { /// 模块单例 public static Single I => instance; /// 模块单例 protected static Single instance; /// 初始化 protected abstract void Awake(); /// 绑定文档 public UIDocument document; /// 根目录文档 public VisualElement root => document.rootVisualElement; /// 绑定文档 public abstract VisualElement Element { get; } /// 查询UI元素 public T Q(string name = null, string className = null) where T : VisualElement => Element.Q(name, className); /// 替换,并且设置切换场景不销毁 protected virtual void Replace(bool isDontDestroy = true) { if (instance != null) { Destroy(instance.gameObject); } instance = (Single)this; if (isDontDestroy) { DontDestroyOnLoad(gameObject); } } /// 不替换,并且设置切换场景不销毁 protected virtual void NoReplace(bool isDontDestroy = true) { if (isDontDestroy) { DontDestroyOnLoad(gameObject); } if (instance == null) { instance = (Single)this; } else { Destroy(gameObject); } } } }