UIManager.cs 4.3 KB

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