123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Rokid.MRC
- {
- public class UIManager : UnitySingleton<UIManager>
- {
- public bool HideUISystem;
- public List<GameObject> UIPrefabList;
- private Dictionary<UIType, UIPanelBase> UIPanelDic = new Dictionary<UIType, UIPanelBase>();
- private List<UIType> OpendUIPanels = new List<UIType>();
- public void Init()
- {
- }
- public UIPanelBase CreatePanel(UIType type)
- {
- UIData uiData = GetUIDataByType(type);
- if(uiData != null)
- {
- return CreatePanel(uiData);
- }
- return null;
- }
- private UIPanelBase CreatePanel(UIData uiData)
- {
- if(GetLoadedPanel(uiData.uiType) != null)
- {
- Debug.LogError($"CreatePanel {uiData.uiType} Already Created");
- }
- if(uiData != null)
- {
- //todo,目前直接从子物体下找
- GameObject uiObj = transform.Find(uiData.uiPrefabPath)?.gameObject;
- if(uiObj == null)
- {
- uiObj = GetUIPrefab(uiData);
- }
- if(uiObj == null)
- {
- Debug.LogError("No Panel In Hierarchy Or In Reference");
- return null;
- }
- uiObj = Instantiate<GameObject>(uiObj, transform);
- UIPanelBase panel = uiObj.GetComponent<UIPanelBase>();
- panel.GetComponent<Canvas>().sortingOrder = uiData.uiDepth;
- panel.OnInit();
- UIPanelDic.Add(uiData.uiType, panel);
- return panel;
- }
- return null;
- }
- private GameObject GetUIPrefab(UIData uiData)
- {
- foreach(var obj in UIPrefabList)
- {
- if(obj.GetComponent<UIPanelBase>().GetType().Name == uiData.uiPrefabPath)
- {
- return obj;
- }
- }
- return null;
- }
- public UIPanelBase GetLoadedPanel(UIType type)
- {
- UIPanelDic.TryGetValue(type, out UIPanelBase panel);
- return panel;
- }
- //todo,先简单写一下
- public UIPanelBase OpenPanel(UIType uiType)
- {
- if(HideUISystem)
- {
- return null;
- }
- UIData uIData = GetUIDataByType(uiType);
- //创建界面
- UIPanelDic.TryGetValue(uiType, out UIPanelBase panel);
- if(panel == null)
- {
- panel = CreatePanel(uIData);
- }
- DoRelationShip(uIData);
- return panel;
- }
- private void DoRelationShip(UIData uIData)
- {
- foreach(var panelPair in UIPanelDic)
- {
- if(panelPair.Key == uIData.uiType)
- {
- panelPair.Value.gameObject.SetActive(true);
- panelPair.Value.OnOpen();
- }
- else
- {
- UIData otherData = GetUIDataByType(panelPair.Key);
- if(otherData != null && !otherData.ignoreOpen)
- {
- panelPair.Value.OnClose();
- panelPair.Value.gameObject.SetActive(false);
- }
- }
- }
- }
- public void ClosePanel(UIType uiType)
- {
- foreach(var panelPair in UIPanelDic)
- {
- if(panelPair.Key == uiType)
- {
- panelPair.Value.OnClose();
- panelPair.Value.gameObject.SetActive(false);
- }
- }
- }
- public void CloseAllPanel()
- {
- foreach(var panelPair in UIPanelDic)
- {
- UIData uIData = GetUIDataByType(panelPair.Key);
- if(uIData != null && !uIData.ignoreOpen)
- {
- panelPair.Value.OnClose();
- panelPair.Value.gameObject.SetActive(false);
- }
- }
- }
- public UIData GetUIDataByType(UIType type)
- {
- foreach(UIData uiData in UIDefines.UIDataList)
- {
- if(uiData.uiType == type)
- {
- return uiData;
- }
- }
- return null;
- }
- public void ShowTips(string desc)
- {
- UITipsPanel tipsPanel = (UITipsPanel)GetLoadedPanel(UIType.Tips);
- tipsPanel?.SetDesc(desc);
- tipsPanel?.gameObject.SetActive(true);
- }
- public void ShowConfirm(string title, string content, ConfirmType confirmType, Action confirmCall = null, Action cancelCall = null)
- {
- UIConfirmPanel confirmPanel = (UIConfirmPanel)OpenPanel(UIType.Confirm);
- confirmPanel?.SetContent(title, content, 3, confirmType, confirmCall, cancelCall);
- confirmPanel?.gameObject.SetActive(true);
- }
- }
- }
|