UIManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Cysharp.Threading.Tasks;
  2. using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class UIManager : MonoBehaviour
  7. {
  8. public static UIManager Instance { get; private set; }
  9. /// <summary> 已实例化UI列表 </summary>
  10. private Dictionary<string, GameObject> m_UIList;
  11. /// <summary> UI资源引用列表 </summary>
  12. private Dictionary<string, GameObject> m_UIPreList=new Dictionary<string, GameObject>();
  13. /// <summary> UI根节点 </summary>
  14. Canvas m_UIRoot;
  15. /// <summary> 已打开UI列表 </summary>
  16. private Dictionary<string, GameObject> m_ShowList;
  17. private void Awake()
  18. {
  19. Instance = this;
  20. m_UIRoot = GetComponentInChildren<Canvas>();
  21. //初始化已实例化UI列表
  22. if(m_UIList==null)
  23. m_UIList = new Dictionary<string, GameObject>();
  24. m_UIList = GetAllGeneratedUI();
  25. }
  26. private void Update()
  27. {
  28. if(m_ShowList == null)return;
  29. bool isMain = false;
  30. foreach (var item in m_ShowList)
  31. {
  32. //Debug.Log(item.Value);
  33. if(item.Value!=null)
  34. if (item.Value.name.Contains("Main")&& m_ShowList.Count==1)
  35. {
  36. isMain = true;
  37. }
  38. }
  39. SetCursor(!isMain);
  40. if(Input.GetKeyDown(KeyCode.Tab))
  41. {
  42. int index = PlayerPrefs.GetInt("CurLanguage");
  43. index= index==0 ? 1 : 0;
  44. LanguageMatchManager.Instance.RefreshLanguage(index);
  45. }
  46. }
  47. public void SetCursor(bool _cursorState)
  48. {
  49. //隐藏鼠标
  50. Cursor.visible = _cursorState;
  51. Cursor.lockState = _cursorState ? CursorLockMode.Confined : CursorLockMode.Locked;
  52. }
  53. /// <summary>
  54. /// 获得已经在根节点上生成的UI
  55. /// </summary>
  56. /// <returns></returns>
  57. private Dictionary<string,GameObject> GetAllGeneratedUI()
  58. {
  59. Dictionary<string,GameObject> tempDic = new Dictionary<string,GameObject>();
  60. for(int i = 0; i < m_UIRoot.transform.childCount; i++)
  61. {
  62. //Debug.Log(m_UIRoot.transform.GetChild(i).name);
  63. tempDic.Add(m_UIRoot.transform.GetChild(i).name, m_UIRoot.transform.GetChild(i).gameObject);
  64. }
  65. return tempDic;
  66. }
  67. /// <summary>
  68. /// 显示UI
  69. /// </summary>
  70. /// <param name="uiName"></param>
  71. public async UniTask ShowUI(WindowID windowID)
  72. {
  73. string uiName = windowID.ToString();
  74. GameObject go;
  75. if (m_UIList.TryGetValue(uiName,out go))
  76. {
  77. go.SetActive(true);
  78. go.GetComponent<BasePanel>()?.OnShowWindow();
  79. }
  80. else
  81. {
  82. go = await CreateAndShowUI(uiName);
  83. }
  84. if (m_ShowList == null)
  85. m_ShowList = new Dictionary<string, GameObject>();
  86. if(!m_ShowList.ContainsKey(uiName))
  87. m_ShowList.Add(uiName, go);
  88. }
  89. /// <summary>
  90. /// 隐藏UI
  91. /// </summary>
  92. /// <param name="uiName"></param>
  93. public void HideUI(WindowID windowID)
  94. {
  95. string uiName = windowID.ToString();
  96. GameObject go;
  97. if(m_UIList.TryGetValue(uiName,out go))
  98. {
  99. go.GetComponent<BasePanel>()?.OnCloseWindow();
  100. go.SetActive(false);
  101. }
  102. m_ShowList.Remove(uiName);
  103. }
  104. /// <summary>
  105. /// 隐藏所有UI
  106. /// </summary>
  107. public void HideAllUI()
  108. {
  109. foreach (var ui in m_UIList)
  110. {
  111. ui.Value.SetActive(false);
  112. }
  113. }
  114. /// <summary>
  115. /// 销毁UI
  116. /// </summary>
  117. /// <param name="uiName"></param>
  118. public void DestroyUI(WindowID windowID)
  119. {
  120. string uiName = windowID.ToString();
  121. GameObject go;
  122. if (m_UIList.TryGetValue(uiName, out go))
  123. {
  124. go.GetComponent<BasePanel>()?.OnDestroyWindow();
  125. Destroy(go);
  126. m_UIList.Remove(uiName);
  127. }
  128. }
  129. /// <summary>
  130. /// 销毁所有UI
  131. /// </summary>
  132. public void DestroyAllUI()
  133. {
  134. List<GameObject> ulist = new List<GameObject>(m_UIList.Values);
  135. for(int i = 0; i < ulist.Count; i++)
  136. {
  137. Destroy(ulist[i]);
  138. }
  139. m_UIList.Clear();
  140. }
  141. /// <summary>
  142. /// 加载并显示UI
  143. /// </summary>
  144. /// <param name="uiName"></param>
  145. private async UniTask<GameObject> CreateAndShowUI(string uiName)
  146. {
  147. //if (m_UIPreList.ContainsKey(uiName))
  148. //{
  149. // GameObject ui = Instantiate(m_UIPreList[uiName]);
  150. // ui.GetComponent<BasePanel>()?.OnInitWindow();
  151. // ui.transform.SetParent(m_UIRoot.transform);
  152. // RectTransform rect = ui.GetComponent<RectTransform>();
  153. // rect.localPosition = Vector3.zero;
  154. // rect.localRotation = Quaternion.identity;
  155. // rect.localScale = Vector3.one;
  156. // m_UIList.Add(uiName, ui);
  157. //}
  158. GameObject go = await YooAssetManager.Instance.LoadAsset<GameObject>(uiName);
  159. if (go != null)
  160. {
  161. GameObject ui = Instantiate(go);
  162. ui.GetComponent<BasePanel>()?.OnInitWindow();
  163. ui.transform.SetParent(m_UIRoot.transform);
  164. RectTransform rect = ui.GetComponent<RectTransform>();
  165. rect.localPosition = Vector3.zero;
  166. rect.offsetMax = Vector2.zero;
  167. rect.offsetMin = Vector2.zero;
  168. rect.localRotation = Quaternion.identity;
  169. rect.localScale = Vector3.one;
  170. m_UIList.Add(uiName, ui);
  171. }
  172. return go;
  173. }
  174. }