using System.Transactions; using Rokid.UXR.Utility; using UnityEngine; namespace Rokid.UXR.UI { public abstract class BaseUI : MonoBehaviour, IUI { private Transform uiParent; private Transform dialogParent; /// /// 数据池缓存 /// protected DataCache data; protected virtual void Awake() { AutoInjectComponent.AutoInject(transform, this); data = DataCache.Instance; } protected virtual void Start() { } protected virtual void OnDestroy() { } public string GetPanelPath() { return "UI/Panel"; } public string GetItemPath() { return "UI/Item"; } /// /// 创建UI /// /// /// 是否是对话框,如果是对话框则不会隐藏上层界面 /// public T CreatePanel(bool dialog = false, string prefabName = null, bool findExitUI = true) where T : BasePanel { return CreatePanel(dialog ? GetDialogParent() : GetUIParent(), dialog, prefabName, findExitUI); } public bool ExistDialog() { return GetUIFromParent(GetDialogParent()) != null; } public bool ExistPanel() { return GetUIFromParent(GetUIParent()) != null; } public T CreatePanel(Transform parent, bool dialog = false, string prefabName = null, bool findExitUI = true) where T : BasePanel { if (parent.childCount >= 1 && dialog == false) { Transform tsf = parent.GetChild(parent.childCount - 1); if (tsf != null) { tsf.gameObject.SetActive(false); } } Transform uiTransform = null; if (findExitUI) { uiTransform = GetUIFromParent(parent); } if (uiTransform != null) { uiTransform.SetAsLastSibling(); uiTransform.gameObject.SetActive(true); return uiTransform.GetComponent(); } GameObject go = null; if (!string.IsNullOrEmpty(prefabName)) { go = Resources.Load(GetPanelPath() + "/" + prefabName); } else { go = Resources.Load(GetPanelPath() + "/" + typeof(T).Name); } if (go != null) { go.SetActive(true); Transform tsf = Instantiate(go, parent).transform; tsf.name = typeof(T).Name; RKLog.Info("创建一个Panel:" + tsf.gameObject.name); return tsf.GetComponent(); } else { RKLog.Warning("找不到加载的路径:" + GetPanelPath() + "/" + typeof(T).Name); return default; } } public T CreateItem(Transform parent, bool active = true) where T : BaseItem { GameObject go; go = Resources.Load(GetItemPath() + "/" + typeof(T).Name); if (go != null) { go.SetActive(active); Transform tsf = Instantiate(go, parent).transform; tsf.localScale = Vector3.one; tsf.name = typeof(T).Name; return tsf.GetComponent(); } else { RKLog.Warning("找不到加载的路径:" + GetItemPath() + "/" + typeof(T).Name); return default; } } public T CreateItem(string prefabName, Transform parent, bool active = true) where T : BaseItem { if (string.IsNullOrEmpty(prefabName)) { return CreateItem(parent, active); } GameObject go; go = Resources.Load(GetItemPath() + "/" + prefabName); if (go != null) { go.SetActive(active); Transform tsf = Instantiate(go, parent).transform; tsf.localScale = Vector3.one; tsf.name = prefabName; return tsf.GetComponent(); } else { RKLog.Warning("找不到加载的路径:" + GetItemPath() + "/" + typeof(T).Name); return default; } } public Transform CreateItem(string prefabName, Transform parent, bool active = true) { GameObject go; go = Resources.Load(GetItemPath() + "/" + prefabName); if (go != null) { go.SetActive(active); Transform tsf = Instantiate(go, parent).transform; tsf.name = prefabName; tsf.localScale = Vector3.one; return tsf; } return null; } /// /// 退出Panel /// public void ExitPanel() { if (GetUIParent().childCount >= 2) { GetUIParent().GetChild(GetUIParent().childCount - 2).gameObject.SetActive(true); Destroy(GetUIParent().GetChild(GetUIParent().childCount - 1).gameObject); } } /// /// 退出panel /// /// public void ExitPanel() where T : BasePanel { Transform tsf = GetUIParent()?.Find(typeof(T).Name); if (tsf != null) { DestroyImmediate(tsf.gameObject); } //将上一个界面的UI设置为true Transform ui = FindTopChild(GetUIParent()); if (ui != null) { ui.gameObject.SetActive(true); } } /// /// 退出panel,不销毁 /// /// public void ExitPanelNotDestroy() where T : BasePanel { Transform tsf = GetUIParent()?.Find(typeof(T).Name); if (tsf != null) { tsf.SetAsFirstSibling(); tsf.gameObject.SetActive(false); } //将上一个界面的UI设置为true Transform ui = FindTopChild(GetUIParent()); if (ui != null) { ui.gameObject.SetActive(true); } } /// /// 退出dialog /// /// public void ExitDialog() where T : IDialog { Transform tsf = GetDialogParent()?.Find(typeof(T).Name); if (tsf != null) { #if UNITY_EDITOR DestroyImmediate(tsf.gameObject); #else Destroy(tsf.gameObject); #endif } } /// /// 找到位于栈顶的UI /// /// /// private Transform FindTopChild(Transform tsf) { if (tsf.childCount > 0) { return tsf.GetChild(tsf.childCount - 1); } return null; } public void ExitItem() where T : BaseItem { Destroy(this.gameObject); } /// /// 退出所有 /// public void ExitAll() { Transform uiParent = GetUIParent(); for (int i = uiParent.childCount - 1; i > 0; i--) { if (Utils.IsAndroidPlatfrom()) { Destroy(uiParent.GetChild(i)); } else { if (uiParent?.GetChild(i) != null) DestroyImmediate(uiParent.GetChild(i)); } } } /// /// 退出所有对话框 /// public void ExitAllDialog() { Transform dialogParent = GetDialogParent(); for (int i = dialogParent.childCount - 1; i > 0; i--) { if (Utils.IsAndroidPlatfrom()) { Destroy(dialogParent.GetChild(i)); } else { if (dialogParent?.GetChild(i) != null) DestroyImmediate(dialogParent.GetChild(i)); } } } /// /// 从Parent中获得 /// /// /// private Transform GetUIFromParent(Transform parent) { Transform tsf = parent.Find(typeof(T).Name); return tsf; } public Transform GetUIParent() { if (uiParent == null) { uiParent = GameObject.FindWithTag("UIParent")?.transform; } if (uiParent == null) { uiParent = transform.Find("UICanvas/UIParent"); } if (uiParent == null) { GameObject go = GameObject.Instantiate(Resources.Load("UI/UICanvas")); go.name = "UICanvas"; uiParent = GameObject.FindWithTag("UIParent")?.transform; } return uiParent; } public Transform GetDialogParent() { if (dialogParent == null) { dialogParent = GameObject.FindWithTag("DialogParent")?.transform; } if (dialogParent == null) { dialogParent = transform.Find("UICanvas/DialogParent"); } if (dialogParent == null) { GameObject go = GameObject.Instantiate(Resources.Load("UI/UICanvas")); go.name = "UICanvas"; dialogParent = GameObject.FindWithTag("DialogParent")?.transform; } return dialogParent; } } }