UIManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Rokid.MRC
  6. {
  7. public class UIManager : UnitySingleton<UIManager>
  8. {
  9. public bool HideUISystem;
  10. public List<GameObject> UIPrefabList;
  11. private Dictionary<UIType, UIPanelBase> UIPanelDic = new Dictionary<UIType, UIPanelBase>();
  12. private List<UIType> OpendUIPanels = new List<UIType>();
  13. public void Init()
  14. {
  15. }
  16. public UIPanelBase CreatePanel(UIType type)
  17. {
  18. UIData uiData = GetUIDataByType(type);
  19. if(uiData != null)
  20. {
  21. return CreatePanel(uiData);
  22. }
  23. return null;
  24. }
  25. private UIPanelBase CreatePanel(UIData uiData)
  26. {
  27. if(GetLoadedPanel(uiData.uiType) != null)
  28. {
  29. Debug.LogError($"CreatePanel {uiData.uiType} Already Created");
  30. }
  31. if(uiData != null)
  32. {
  33. //todo,目前直接从子物体下找
  34. GameObject uiObj = transform.Find(uiData.uiPrefabPath)?.gameObject;
  35. if(uiObj == null)
  36. {
  37. uiObj = GetUIPrefab(uiData);
  38. }
  39. if(uiObj == null)
  40. {
  41. Debug.LogError("No Panel In Hierarchy Or In Reference");
  42. return null;
  43. }
  44. uiObj = Instantiate<GameObject>(uiObj, transform);
  45. UIPanelBase panel = uiObj.GetComponent<UIPanelBase>();
  46. panel.GetComponent<Canvas>().sortingOrder = uiData.uiDepth;
  47. panel.OnInit();
  48. UIPanelDic.Add(uiData.uiType, panel);
  49. return panel;
  50. }
  51. return null;
  52. }
  53. private GameObject GetUIPrefab(UIData uiData)
  54. {
  55. foreach(var obj in UIPrefabList)
  56. {
  57. if(obj.GetComponent<UIPanelBase>().GetType().Name == uiData.uiPrefabPath)
  58. {
  59. return obj;
  60. }
  61. }
  62. return null;
  63. }
  64. public UIPanelBase GetLoadedPanel(UIType type)
  65. {
  66. UIPanelDic.TryGetValue(type, out UIPanelBase panel);
  67. return panel;
  68. }
  69. //todo,先简单写一下
  70. public UIPanelBase OpenPanel(UIType uiType)
  71. {
  72. if(HideUISystem)
  73. {
  74. return null;
  75. }
  76. UIData uIData = GetUIDataByType(uiType);
  77. //创建界面
  78. UIPanelDic.TryGetValue(uiType, out UIPanelBase panel);
  79. if(panel == null)
  80. {
  81. panel = CreatePanel(uIData);
  82. }
  83. DoRelationShip(uIData);
  84. return panel;
  85. }
  86. private void DoRelationShip(UIData uIData)
  87. {
  88. foreach(var panelPair in UIPanelDic)
  89. {
  90. if(panelPair.Key == uIData.uiType)
  91. {
  92. panelPair.Value.gameObject.SetActive(true);
  93. panelPair.Value.OnOpen();
  94. }
  95. else
  96. {
  97. UIData otherData = GetUIDataByType(panelPair.Key);
  98. if(otherData != null && !otherData.ignoreOpen)
  99. {
  100. panelPair.Value.OnClose();
  101. panelPair.Value.gameObject.SetActive(false);
  102. }
  103. }
  104. }
  105. }
  106. public void ClosePanel(UIType uiType)
  107. {
  108. foreach(var panelPair in UIPanelDic)
  109. {
  110. if(panelPair.Key == uiType)
  111. {
  112. panelPair.Value.OnClose();
  113. panelPair.Value.gameObject.SetActive(false);
  114. }
  115. }
  116. }
  117. public void CloseAllPanel()
  118. {
  119. foreach(var panelPair in UIPanelDic)
  120. {
  121. UIData uIData = GetUIDataByType(panelPair.Key);
  122. if(uIData != null && !uIData.ignoreOpen)
  123. {
  124. panelPair.Value.OnClose();
  125. panelPair.Value.gameObject.SetActive(false);
  126. }
  127. }
  128. }
  129. public UIData GetUIDataByType(UIType type)
  130. {
  131. foreach(UIData uiData in UIDefines.UIDataList)
  132. {
  133. if(uiData.uiType == type)
  134. {
  135. return uiData;
  136. }
  137. }
  138. return null;
  139. }
  140. public void ShowTips(string desc)
  141. {
  142. UITipsPanel tipsPanel = (UITipsPanel)GetLoadedPanel(UIType.Tips);
  143. tipsPanel?.SetDesc(desc);
  144. tipsPanel?.gameObject.SetActive(true);
  145. }
  146. public void ShowConfirm(string title, string content, ConfirmType confirmType, Action confirmCall = null, Action cancelCall = null)
  147. {
  148. UIConfirmPanel confirmPanel = (UIConfirmPanel)OpenPanel(UIType.Confirm);
  149. confirmPanel?.SetContent(title, content, 3, confirmType, confirmCall, cancelCall);
  150. confirmPanel?.gameObject.SetActive(true);
  151. }
  152. }
  153. }