123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- 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;
- /// <summary>
- /// 数据池缓存
- /// </summary>
- 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";
- }
- /// <summary>
- /// 创建UI
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dialog">是否是对话框,如果是对话框则不会隐藏上层界面</param>
- /// <returns></returns>
- public T CreatePanel<T>(bool dialog = false, string prefabName = null, bool findExitUI = true) where T : BasePanel
- {
- return CreatePanel<T>(dialog ? GetDialogParent() : GetUIParent(), dialog, prefabName, findExitUI);
- }
- public bool ExistDialog<T>()
- {
- return GetUIFromParent<T>(GetDialogParent()) != null;
- }
- public bool ExistPanel<T>()
- {
- return GetUIFromParent<T>(GetUIParent()) != null;
- }
- public T CreatePanel<T>(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<T>(parent);
- }
- if (uiTransform != null)
- {
- uiTransform.SetAsLastSibling();
- uiTransform.gameObject.SetActive(true);
- return uiTransform.GetComponent<T>();
- }
- GameObject go = null;
- if (!string.IsNullOrEmpty(prefabName))
- {
- go = Resources.Load<GameObject>(GetPanelPath() + "/" + prefabName);
- }
- else
- {
- go = Resources.Load<GameObject>(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<T>();
- }
- else
- {
- RKLog.Warning("找不到加载的路径:" + GetPanelPath() + "/" + typeof(T).Name);
- return default;
- }
- }
- public T CreateItem<T>(Transform parent, bool active = true) where T : BaseItem
- {
- GameObject go;
- go = Resources.Load<GameObject>(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<T>();
- }
- else
- {
- RKLog.Warning("找不到加载的路径:" + GetItemPath() + "/" + typeof(T).Name);
- return default;
- }
- }
- public T CreateItem<T>(string prefabName, Transform parent, bool active = true) where T : BaseItem
- {
- if (string.IsNullOrEmpty(prefabName))
- {
- return CreateItem<T>(parent, active);
- }
- GameObject go;
- go = Resources.Load<GameObject>(GetItemPath() + "/" + prefabName);
- if (go != null)
- {
- go.SetActive(active);
- Transform tsf = Instantiate(go, parent).transform;
- tsf.localScale = Vector3.one;
- tsf.name = prefabName;
- return tsf.GetComponent<T>();
- }
- else
- {
- RKLog.Warning("找不到加载的路径:" + GetItemPath() + "/" + typeof(T).Name);
- return default;
- }
- }
- public Transform CreateItem(string prefabName, Transform parent, bool active = true)
- {
- GameObject go;
- go = Resources.Load<GameObject>(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;
- }
- /// <summary>
- /// 退出Panel
- /// </summary>
- public void ExitPanel()
- {
- if (GetUIParent().childCount >= 2)
- {
- GetUIParent().GetChild(GetUIParent().childCount - 2).gameObject.SetActive(true);
- Destroy(GetUIParent().GetChild(GetUIParent().childCount - 1).gameObject);
- }
- }
- /// <summary>
- /// 退出panel
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public void ExitPanel<T>() 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);
- }
- }
- /// <summary>
- /// 退出panel,不销毁
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public void ExitPanelNotDestroy<T>() 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);
- }
- }
- /// <summary>
- /// 退出dialog
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public void ExitDialog<T>() where T : IDialog
- {
- Transform tsf = GetDialogParent()?.Find(typeof(T).Name);
- if (tsf != null)
- {
- #if UNITY_EDITOR
- DestroyImmediate(tsf.gameObject);
- #else
- Destroy(tsf.gameObject);
- #endif
- }
- }
- /// <summary>
- /// 找到位于栈顶的UI
- /// </summary>
- /// <param name="tsf"></param>
- /// <returns></returns>
- private Transform FindTopChild(Transform tsf)
- {
- if (tsf.childCount > 0)
- {
- return tsf.GetChild(tsf.childCount - 1);
- }
- return null;
- }
- public void ExitItem<T>() where T : BaseItem
- {
- Destroy(this.gameObject);
- }
- /// <summary>
- /// 退出所有
- /// </summary>
- 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));
- }
- }
- }
- /// <summary>
- /// 退出所有对话框
- /// </summary>
- 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));
- }
- }
- }
- /// <summary>
- /// 从Parent中获得
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- private Transform GetUIFromParent<T>(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<GameObject>("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<GameObject>("UI/UICanvas"));
- go.name = "UICanvas";
- dialogParent = GameObject.FindWithTag("DialogParent")?.transform;
- }
- return dialogParent;
- }
- }
- }
|