BaseUI.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System.Transactions;
  2. using Rokid.UXR.Utility;
  3. using UnityEngine;
  4. namespace Rokid.UXR.UI
  5. {
  6. public abstract class BaseUI : MonoBehaviour, IUI
  7. {
  8. private Transform uiParent;
  9. private Transform dialogParent;
  10. /// <summary>
  11. /// 数据池缓存
  12. /// </summary>
  13. protected DataCache data;
  14. protected virtual void Awake()
  15. {
  16. AutoInjectComponent.AutoInject(transform, this);
  17. data = DataCache.Instance;
  18. }
  19. protected virtual void Start()
  20. {
  21. }
  22. protected virtual void OnDestroy()
  23. {
  24. }
  25. public string GetPanelPath()
  26. {
  27. return "UI/Panel";
  28. }
  29. public string GetItemPath()
  30. {
  31. return "UI/Item";
  32. }
  33. /// <summary>
  34. /// 创建UI
  35. /// </summary>
  36. /// <typeparam name="T"></typeparam>
  37. /// <param name="dialog">是否是对话框,如果是对话框则不会隐藏上层界面</param>
  38. /// <returns></returns>
  39. public T CreatePanel<T>(bool dialog = false, string prefabName = null, bool findExitUI = true) where T : BasePanel
  40. {
  41. return CreatePanel<T>(dialog ? GetDialogParent() : GetUIParent(), dialog, prefabName, findExitUI);
  42. }
  43. public bool ExistDialog<T>()
  44. {
  45. return GetUIFromParent<T>(GetDialogParent()) != null;
  46. }
  47. public bool ExistPanel<T>()
  48. {
  49. return GetUIFromParent<T>(GetUIParent()) != null;
  50. }
  51. public T CreatePanel<T>(Transform parent, bool dialog = false, string prefabName = null, bool findExitUI = true) where T : BasePanel
  52. {
  53. if (parent.childCount >= 1 && dialog == false)
  54. {
  55. Transform tsf = parent.GetChild(parent.childCount - 1);
  56. if (tsf != null)
  57. {
  58. tsf.gameObject.SetActive(false);
  59. }
  60. }
  61. Transform uiTransform = null;
  62. if (findExitUI)
  63. {
  64. uiTransform = GetUIFromParent<T>(parent);
  65. }
  66. if (uiTransform != null)
  67. {
  68. uiTransform.SetAsLastSibling();
  69. uiTransform.gameObject.SetActive(true);
  70. return uiTransform.GetComponent<T>();
  71. }
  72. GameObject go = null;
  73. if (!string.IsNullOrEmpty(prefabName))
  74. {
  75. go = Resources.Load<GameObject>(GetPanelPath() + "/" + prefabName);
  76. }
  77. else
  78. {
  79. go = Resources.Load<GameObject>(GetPanelPath() + "/" + typeof(T).Name);
  80. }
  81. if (go != null)
  82. {
  83. go.SetActive(true);
  84. Transform tsf = Instantiate(go, parent).transform;
  85. tsf.name = typeof(T).Name;
  86. RKLog.Info("创建一个Panel:" + tsf.gameObject.name);
  87. return tsf.GetComponent<T>();
  88. }
  89. else
  90. {
  91. RKLog.Warning("找不到加载的路径:" + GetPanelPath() + "/" + typeof(T).Name);
  92. return default;
  93. }
  94. }
  95. public T CreateItem<T>(Transform parent, bool active = true) where T : BaseItem
  96. {
  97. GameObject go;
  98. go = Resources.Load<GameObject>(GetItemPath() + "/" + typeof(T).Name);
  99. if (go != null)
  100. {
  101. go.SetActive(active);
  102. Transform tsf = Instantiate(go, parent).transform;
  103. tsf.localScale = Vector3.one;
  104. tsf.name = typeof(T).Name;
  105. return tsf.GetComponent<T>();
  106. }
  107. else
  108. {
  109. RKLog.Warning("找不到加载的路径:" + GetItemPath() + "/" + typeof(T).Name);
  110. return default;
  111. }
  112. }
  113. public T CreateItem<T>(string prefabName, Transform parent, bool active = true) where T : BaseItem
  114. {
  115. if (string.IsNullOrEmpty(prefabName))
  116. {
  117. return CreateItem<T>(parent, active);
  118. }
  119. GameObject go;
  120. go = Resources.Load<GameObject>(GetItemPath() + "/" + prefabName);
  121. if (go != null)
  122. {
  123. go.SetActive(active);
  124. Transform tsf = Instantiate(go, parent).transform;
  125. tsf.localScale = Vector3.one;
  126. tsf.name = prefabName;
  127. return tsf.GetComponent<T>();
  128. }
  129. else
  130. {
  131. RKLog.Warning("找不到加载的路径:" + GetItemPath() + "/" + typeof(T).Name);
  132. return default;
  133. }
  134. }
  135. public Transform CreateItem(string prefabName, Transform parent, bool active = true)
  136. {
  137. GameObject go;
  138. go = Resources.Load<GameObject>(GetItemPath() + "/" + prefabName);
  139. if (go != null)
  140. {
  141. go.SetActive(active);
  142. Transform tsf = Instantiate(go, parent).transform;
  143. tsf.name = prefabName;
  144. tsf.localScale = Vector3.one;
  145. return tsf;
  146. }
  147. return null;
  148. }
  149. /// <summary>
  150. /// 退出Panel
  151. /// </summary>
  152. public void ExitPanel()
  153. {
  154. if (GetUIParent().childCount >= 2)
  155. {
  156. GetUIParent().GetChild(GetUIParent().childCount - 2).gameObject.SetActive(true);
  157. Destroy(GetUIParent().GetChild(GetUIParent().childCount - 1).gameObject);
  158. }
  159. }
  160. /// <summary>
  161. /// 退出panel
  162. /// </summary>
  163. /// <typeparam name="T"></typeparam>
  164. public void ExitPanel<T>() where T : BasePanel
  165. {
  166. Transform tsf = GetUIParent()?.Find(typeof(T).Name);
  167. if (tsf != null)
  168. {
  169. DestroyImmediate(tsf.gameObject);
  170. }
  171. //将上一个界面的UI设置为true
  172. Transform ui = FindTopChild(GetUIParent());
  173. if (ui != null)
  174. {
  175. ui.gameObject.SetActive(true);
  176. }
  177. }
  178. /// <summary>
  179. /// 退出panel,不销毁
  180. /// </summary>
  181. /// <typeparam name="T"></typeparam>
  182. public void ExitPanelNotDestroy<T>() where T : BasePanel
  183. {
  184. Transform tsf = GetUIParent()?.Find(typeof(T).Name);
  185. if (tsf != null)
  186. {
  187. tsf.SetAsFirstSibling();
  188. tsf.gameObject.SetActive(false);
  189. }
  190. //将上一个界面的UI设置为true
  191. Transform ui = FindTopChild(GetUIParent());
  192. if (ui != null)
  193. {
  194. ui.gameObject.SetActive(true);
  195. }
  196. }
  197. /// <summary>
  198. /// 退出dialog
  199. /// </summary>
  200. /// <typeparam name="T"></typeparam>
  201. public void ExitDialog<T>() where T : IDialog
  202. {
  203. Transform tsf = GetDialogParent()?.Find(typeof(T).Name);
  204. if (tsf != null)
  205. {
  206. #if UNITY_EDITOR
  207. DestroyImmediate(tsf.gameObject);
  208. #else
  209. Destroy(tsf.gameObject);
  210. #endif
  211. }
  212. }
  213. /// <summary>
  214. /// 找到位于栈顶的UI
  215. /// </summary>
  216. /// <param name="tsf"></param>
  217. /// <returns></returns>
  218. private Transform FindTopChild(Transform tsf)
  219. {
  220. if (tsf.childCount > 0)
  221. {
  222. return tsf.GetChild(tsf.childCount - 1);
  223. }
  224. return null;
  225. }
  226. public void ExitItem<T>() where T : BaseItem
  227. {
  228. Destroy(this.gameObject);
  229. }
  230. /// <summary>
  231. /// 退出所有
  232. /// </summary>
  233. public void ExitAll()
  234. {
  235. Transform uiParent = GetUIParent();
  236. for (int i = uiParent.childCount - 1; i > 0; i--)
  237. {
  238. if (Utils.IsAndroidPlatfrom())
  239. {
  240. Destroy(uiParent.GetChild(i));
  241. }
  242. else
  243. {
  244. if (uiParent?.GetChild(i) != null)
  245. DestroyImmediate(uiParent.GetChild(i));
  246. }
  247. }
  248. }
  249. /// <summary>
  250. /// 退出所有对话框
  251. /// </summary>
  252. public void ExitAllDialog()
  253. {
  254. Transform dialogParent = GetDialogParent();
  255. for (int i = dialogParent.childCount - 1; i > 0; i--)
  256. {
  257. if (Utils.IsAndroidPlatfrom())
  258. {
  259. Destroy(dialogParent.GetChild(i));
  260. }
  261. else
  262. {
  263. if (dialogParent?.GetChild(i) != null)
  264. DestroyImmediate(dialogParent.GetChild(i));
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// 从Parent中获得
  270. /// </summary>
  271. /// <typeparam name="T"></typeparam>
  272. /// <returns></returns>
  273. private Transform GetUIFromParent<T>(Transform parent)
  274. {
  275. Transform tsf = parent.Find(typeof(T).Name);
  276. return tsf;
  277. }
  278. public Transform GetUIParent()
  279. {
  280. if (uiParent == null)
  281. {
  282. uiParent = GameObject.FindWithTag("UIParent")?.transform;
  283. }
  284. if (uiParent == null)
  285. {
  286. uiParent = transform.Find("UICanvas/UIParent");
  287. }
  288. if (uiParent == null)
  289. {
  290. GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("UI/UICanvas"));
  291. go.name = "UICanvas";
  292. uiParent = GameObject.FindWithTag("UIParent")?.transform;
  293. }
  294. return uiParent;
  295. }
  296. public Transform GetDialogParent()
  297. {
  298. if (dialogParent == null)
  299. {
  300. dialogParent = GameObject.FindWithTag("DialogParent")?.transform;
  301. }
  302. if (dialogParent == null)
  303. {
  304. dialogParent = transform.Find("UICanvas/DialogParent");
  305. }
  306. if (dialogParent == null)
  307. {
  308. GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("UI/UICanvas"));
  309. go.name = "UICanvas";
  310. dialogParent = GameObject.FindWithTag("DialogParent")?.transform;
  311. }
  312. return dialogParent;
  313. }
  314. }
  315. }