UI_LeftTopMenu.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// 左上角菜单
  8. /// </summary>
  9. public class UI_LeftTopMenu : MonoBehaviour
  10. {
  11. private GameObject btnBack = null;
  12. private GameObject btnMain = null;
  13. private bool _showMainUI = false;
  14. private bool _clickClose = true;
  15. private string _closeUIName = "";
  16. private Action<GameObject> _closeAC = null;
  17. /// <summary>
  18. /// 所有的左上角菜单
  19. /// </summary>
  20. private static Dictionary<string, UI_LeftTopMenu> allLeftTopMenus = new Dictionary<string, UI_LeftTopMenu>();
  21. /// <summary>
  22. /// 创建
  23. /// </summary>
  24. /// <param name="parentTran">父Transform</param>
  25. /// <param name="uiClass">界面主类</param>
  26. /// <param name="closeShowMainUI">关闭时是否显示主界面</param>
  27. /// <param name="closeAC">关闭回调</param>
  28. public static void Create(Transform parentTran, MonoBehaviour uiClass, string titleName = "", bool closeShowMainUI = false, Action<GameObject> closeAC = null, bool clickClose = true)
  29. {
  30. ResourceHelper.Instance.LoadAssetBundle("UI_LeftTopMenu", (ab) =>
  31. {
  32. if (ab != null)
  33. {
  34. GameObject uiObj = Instantiate(ab.LoadAsset<GameObject>("UI_LeftTopMenu"), parentTran);
  35. UI_LeftTopMenu leftTopMenu = uiObj.AddComponent<UI_LeftTopMenu>();
  36. leftTopMenu.Init(uiClass.GetType().Name, titleName, closeShowMainUI, closeAC, clickClose);
  37. if (allLeftTopMenus.ContainsKey(uiClass.GetType().Name))
  38. {
  39. allLeftTopMenus[uiClass.GetType().Name] = leftTopMenu;
  40. }
  41. else
  42. {
  43. allLeftTopMenus.Add(uiClass.GetType().Name, leftTopMenu);
  44. }
  45. }
  46. });
  47. }
  48. /// <summary>
  49. /// 设置标题
  50. /// </summary>
  51. /// <param name="titleName"></param>
  52. public static void SetTitle(MonoBehaviour uiClass, string titleName)
  53. {
  54. if (allLeftTopMenus.ContainsKey(uiClass.GetType().Name) && allLeftTopMenus[uiClass.GetType().Name] != null)
  55. {
  56. allLeftTopMenus[uiClass.GetType().Name].SetTitle(titleName);
  57. }
  58. else
  59. {
  60. Debug.Log("查找左上角菜单失败:"+ uiClass.GetType().Name);
  61. }
  62. }
  63. /// <summary>
  64. /// 创建
  65. /// </summary>
  66. /// <param name="parentTran">父Transform</param>
  67. /// <param name="mb">界面主类</param>
  68. /// <param name="closeShowMainUI">关闭时是否显示主界面</param>
  69. /// <param name="closeAC">关闭回调</param>
  70. public static void Create(Transform parentTran, string closeUIName, string titleName = "", bool closeShowMainUI = false, Action<GameObject> closeAC = null, bool clickClose = true)
  71. {
  72. ResourceHelper.Instance.LoadAssetBundle("UI_LeftTopMenu", (ab) =>
  73. {
  74. if (ab != null)
  75. {
  76. GameObject uiObj = Instantiate(ab.LoadAsset<GameObject>("UI_LeftTopMenu"), parentTran);
  77. uiObj.AddComponent<UI_LeftTopMenu>().Init(closeUIName, titleName, closeShowMainUI, closeAC, clickClose);
  78. }
  79. });
  80. }
  81. /// <summary>
  82. /// 初始化
  83. /// </summary>
  84. /// <param name="mb">界面主类</param>
  85. /// <param name="showMainUI">关闭时是否显示主界面</param>
  86. /// <param name="closeAC">关闭回调</param>
  87. private void Init(string closeUIName, string titleName = "", bool closeShowMainUI = false, Action<GameObject> closeAC = null, bool clickClose = true)
  88. {
  89. _closeUIName = closeUIName;
  90. _showMainUI = closeShowMainUI;
  91. _closeAC = closeAC;
  92. _clickClose = clickClose;
  93. Text title = transform.Find("Title/Text").GetComponent<Text>();
  94. if (titleName != "")
  95. {
  96. title.transform.parent.gameObject.SetActive(true);
  97. title.text = titleName;
  98. }
  99. else
  100. {
  101. title.transform.parent.gameObject.SetActive(false);
  102. title.text = "";
  103. }
  104. btnBack = transform.Find("btnClose").gameObject;
  105. EventTriggerListener.Get(btnBack).onClick = Close_Click;
  106. btnMain = transform.Find("btnMain").gameObject;
  107. EventTriggerListener.Get(btnMain).onClick = Main_Click;
  108. }
  109. /// <summary>
  110. /// 设置标题
  111. /// </summary>
  112. /// <param name="titleName"></param>
  113. private void SetTitle(string titleName)
  114. {
  115. Text title = transform.Find("Title/Text").GetComponent<Text>();
  116. if (titleName != "")
  117. {
  118. title.transform.parent.gameObject.SetActive(true);
  119. title.text = titleName;
  120. }
  121. else
  122. {
  123. title.transform.parent.gameObject.SetActive(false);
  124. title.text = "";
  125. }
  126. }
  127. /// <summary>
  128. /// 装备按钮 点击
  129. /// </summary>
  130. /// <param name="go"> 按钮对象 </param>
  131. private void Close_Click(GameObject go)
  132. {
  133. if (_clickClose)
  134. {
  135. PanelHelper.Instance.ClosePanel(_closeUIName);
  136. if (_showMainUI)
  137. {
  138. PanelHelper.Instance.ShowPanel("UI_BaseMainWindow");
  139. }
  140. }
  141. if (Entrance_WorldMap.mainUIBuilder != null)
  142. {
  143. Entrance_WorldMap.mainUIBuilder.gameObject.SetActive(true);
  144. Entrance_WorldMap.mainUIBuilder.InitBuilder();
  145. }
  146. // 回调
  147. if (_closeAC != null)
  148. {
  149. _closeAC.Invoke(go);
  150. }
  151. }
  152. /// <summary>
  153. /// 装备按钮 点击
  154. /// </summary>
  155. /// <param name="go"> 按钮对象 </param>
  156. private void Main_Click(GameObject go)
  157. {
  158. PanelHelper.Instance.CloseAllPanel();
  159. if (GameObject.Find("InWorldMap") == null)
  160. {
  161. LevelManager.Instance().LoadLevel(E_Level.Main);
  162. }
  163. else
  164. {
  165. PanelHelper.Instance.ShowPanel("UI_BaseMainWindow");
  166. Entrance_WorldMap.mainUIBuilder.InitBuilder();
  167. }
  168. }
  169. }