123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.UI;
- /// <summary>
- /// 左上角菜单
- /// </summary>
- public class UI_LeftTopMenu : MonoBehaviour
- {
- private GameObject btnBack = null;
- private GameObject btnMain = null;
- private bool _showMainUI = false;
- private bool _clickClose = true;
- private string _closeUIName = "";
- private Action<GameObject> _closeAC = null;
- /// <summary>
- /// 所有的左上角菜单
- /// </summary>
- private static Dictionary<string, UI_LeftTopMenu> allLeftTopMenus = new Dictionary<string, UI_LeftTopMenu>();
- /// <summary>
- /// 创建
- /// </summary>
- /// <param name="parentTran">父Transform</param>
- /// <param name="uiClass">界面主类</param>
- /// <param name="closeShowMainUI">关闭时是否显示主界面</param>
- /// <param name="closeAC">关闭回调</param>
- public static void Create(Transform parentTran, MonoBehaviour uiClass, string titleName = "", bool closeShowMainUI = false, Action<GameObject> closeAC = null, bool clickClose = true)
- {
- ResourceHelper.Instance.LoadAssetBundle("UI_LeftTopMenu", (ab) =>
- {
- if (ab != null)
- {
- GameObject uiObj = Instantiate(ab.LoadAsset<GameObject>("UI_LeftTopMenu"), parentTran);
- UI_LeftTopMenu leftTopMenu = uiObj.AddComponent<UI_LeftTopMenu>();
- leftTopMenu.Init(uiClass.GetType().Name, titleName, closeShowMainUI, closeAC, clickClose);
- if (allLeftTopMenus.ContainsKey(uiClass.GetType().Name))
- {
- allLeftTopMenus[uiClass.GetType().Name] = leftTopMenu;
- }
- else
- {
- allLeftTopMenus.Add(uiClass.GetType().Name, leftTopMenu);
- }
- }
- });
- }
- /// <summary>
- /// 设置标题
- /// </summary>
- /// <param name="titleName"></param>
- public static void SetTitle(MonoBehaviour uiClass, string titleName)
- {
- if (allLeftTopMenus.ContainsKey(uiClass.GetType().Name) && allLeftTopMenus[uiClass.GetType().Name] != null)
- {
- allLeftTopMenus[uiClass.GetType().Name].SetTitle(titleName);
- }
- else
- {
- Debug.Log("查找左上角菜单失败:"+ uiClass.GetType().Name);
- }
- }
- /// <summary>
- /// 创建
- /// </summary>
- /// <param name="parentTran">父Transform</param>
- /// <param name="mb">界面主类</param>
- /// <param name="closeShowMainUI">关闭时是否显示主界面</param>
- /// <param name="closeAC">关闭回调</param>
- public static void Create(Transform parentTran, string closeUIName, string titleName = "", bool closeShowMainUI = false, Action<GameObject> closeAC = null, bool clickClose = true)
- {
- ResourceHelper.Instance.LoadAssetBundle("UI_LeftTopMenu", (ab) =>
- {
- if (ab != null)
- {
- GameObject uiObj = Instantiate(ab.LoadAsset<GameObject>("UI_LeftTopMenu"), parentTran);
- uiObj.AddComponent<UI_LeftTopMenu>().Init(closeUIName, titleName, closeShowMainUI, closeAC, clickClose);
- }
- });
- }
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="mb">界面主类</param>
- /// <param name="showMainUI">关闭时是否显示主界面</param>
- /// <param name="closeAC">关闭回调</param>
- private void Init(string closeUIName, string titleName = "", bool closeShowMainUI = false, Action<GameObject> closeAC = null, bool clickClose = true)
- {
- _closeUIName = closeUIName;
- _showMainUI = closeShowMainUI;
- _closeAC = closeAC;
- _clickClose = clickClose;
- Text title = transform.Find("Title/Text").GetComponent<Text>();
- if (titleName != "")
- {
- title.transform.parent.gameObject.SetActive(true);
- title.text = titleName;
- }
- else
- {
- title.transform.parent.gameObject.SetActive(false);
- title.text = "";
- }
- btnBack = transform.Find("btnClose").gameObject;
- EventTriggerListener.Get(btnBack).onClick = Close_Click;
- btnMain = transform.Find("btnMain").gameObject;
- EventTriggerListener.Get(btnMain).onClick = Main_Click;
- }
- /// <summary>
- /// 设置标题
- /// </summary>
- /// <param name="titleName"></param>
- private void SetTitle(string titleName)
- {
- Text title = transform.Find("Title/Text").GetComponent<Text>();
- if (titleName != "")
- {
- title.transform.parent.gameObject.SetActive(true);
- title.text = titleName;
- }
- else
- {
- title.transform.parent.gameObject.SetActive(false);
- title.text = "";
- }
- }
- /// <summary>
- /// 装备按钮 点击
- /// </summary>
- /// <param name="go"> 按钮对象 </param>
- private void Close_Click(GameObject go)
- {
- if (_clickClose)
- {
- PanelHelper.Instance.ClosePanel(_closeUIName);
- if (_showMainUI)
- {
- PanelHelper.Instance.ShowPanel("UI_BaseMainWindow");
- }
- }
- if (Entrance_WorldMap.mainUIBuilder != null)
- {
- Entrance_WorldMap.mainUIBuilder.gameObject.SetActive(true);
- Entrance_WorldMap.mainUIBuilder.InitBuilder();
- }
- // 回调
- if (_closeAC != null)
- {
- _closeAC.Invoke(go);
- }
- }
- /// <summary>
- /// 装备按钮 点击
- /// </summary>
- /// <param name="go"> 按钮对象 </param>
- private void Main_Click(GameObject go)
- {
- PanelHelper.Instance.CloseAllPanel();
- if (GameObject.Find("InWorldMap") == null)
- {
- LevelManager.Instance().LoadLevel(E_Level.Main);
- }
- else
- {
- PanelHelper.Instance.ShowPanel("UI_BaseMainWindow");
- Entrance_WorldMap.mainUIBuilder.InitBuilder();
- }
- }
- }
|