using Cysharp.Threading.Tasks; using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIManager : MonoBehaviour { public static UIManager Instance { get; private set; } /// 已实例化UI列表 private Dictionary m_UIList; /// UI资源引用列表 private Dictionary m_UIPreList=new Dictionary(); /// UI根节点 Canvas m_UIRoot; /// 已打开UI列表 private Dictionary m_ShowList; private void Awake() { Instance = this; m_UIRoot = GetComponentInChildren(); //初始化已实例化UI列表 if(m_UIList==null) m_UIList = new Dictionary(); m_UIList = GetAllGeneratedUI(); } private void Update() { if(m_ShowList == null)return; bool isMain = false; foreach (var item in m_ShowList) { //Debug.Log(item.Value); if(item.Value!=null) if (item.Value.name.Contains("Main")&& m_ShowList.Count==1) { isMain = true; } } SetCursor(!isMain); if(Input.GetKeyDown(KeyCode.Tab)) { int index = PlayerPrefs.GetInt("CurLanguage"); index= index==0 ? 1 : 0; LanguageMatchManager.Instance.RefreshLanguage(index); } } public void SetCursor(bool _cursorState) { //隐藏鼠标 Cursor.visible = _cursorState; Cursor.lockState = _cursorState ? CursorLockMode.Confined : CursorLockMode.Locked; } /// /// 获得已经在根节点上生成的UI /// /// private Dictionary GetAllGeneratedUI() { Dictionary tempDic = new Dictionary(); for(int i = 0; i < m_UIRoot.transform.childCount; i++) { //Debug.Log(m_UIRoot.transform.GetChild(i).name); tempDic.Add(m_UIRoot.transform.GetChild(i).name, m_UIRoot.transform.GetChild(i).gameObject); } return tempDic; } /// /// 显示UI /// /// public async UniTask ShowUI(WindowID windowID) { string uiName = windowID.ToString(); GameObject go; if (m_UIList.TryGetValue(uiName,out go)) { go.SetActive(true); go.GetComponent()?.OnShowWindow(); } else { go = await CreateAndShowUI(uiName); } if (m_ShowList == null) m_ShowList = new Dictionary(); if(!m_ShowList.ContainsKey(uiName)) m_ShowList.Add(uiName, go); } /// /// 隐藏UI /// /// public void HideUI(WindowID windowID) { string uiName = windowID.ToString(); GameObject go; if(m_UIList.TryGetValue(uiName,out go)) { go.GetComponent()?.OnCloseWindow(); go.SetActive(false); } m_ShowList.Remove(uiName); } /// /// 隐藏所有UI /// public void HideAllUI() { foreach (var ui in m_UIList) { ui.Value.SetActive(false); } } /// /// 销毁UI /// /// public void DestroyUI(WindowID windowID) { string uiName = windowID.ToString(); GameObject go; if (m_UIList.TryGetValue(uiName, out go)) { go.GetComponent()?.OnDestroyWindow(); Destroy(go); m_UIList.Remove(uiName); } } /// /// 销毁所有UI /// public void DestroyAllUI() { List ulist = new List(m_UIList.Values); for(int i = 0; i < ulist.Count; i++) { Destroy(ulist[i]); } m_UIList.Clear(); } /// /// 加载并显示UI /// /// private async UniTask CreateAndShowUI(string uiName) { //if (m_UIPreList.ContainsKey(uiName)) //{ // GameObject ui = Instantiate(m_UIPreList[uiName]); // ui.GetComponent()?.OnInitWindow(); // ui.transform.SetParent(m_UIRoot.transform); // RectTransform rect = ui.GetComponent(); // rect.localPosition = Vector3.zero; // rect.localRotation = Quaternion.identity; // rect.localScale = Vector3.one; // m_UIList.Add(uiName, ui); //} GameObject go = await YooAssetManager.Instance.LoadAsset(uiName); if (go != null) { GameObject ui = Instantiate(go); ui.GetComponent()?.OnInitWindow(); ui.transform.SetParent(m_UIRoot.transform); RectTransform rect = ui.GetComponent(); rect.localPosition = Vector3.zero; rect.offsetMax = Vector2.zero; rect.offsetMin = Vector2.zero; rect.localRotation = Quaternion.identity; rect.localScale = Vector3.one; m_UIList.Add(uiName, ui); } return go; } }