forked from zxl/Frame
1
0
Fork 0
Frame/Assets/Scripts/UI/UIManager.cs

137 lines
3.3 KiB
C#
Raw Normal View History

2024-04-03 14:38:44 +08:00
using System;
using System.Collections.Generic;
2024-04-08 18:44:01 +08:00
using TMPro;
using UnityEngine;
2024-04-02 18:17:57 +08:00
namespace Game
{
public class UIManager : ManagerBase, IUIManager
{
private Stack<UIBase> _uis = new Stack<UIBase>();
2024-04-03 14:38:44 +08:00
private Dictionary<UIType, UIBase> _uiDic = new Dictionary<UIType, UIBase>();
private Dictionary<UIType, Type> _types = new Dictionary<UIType, Type>();
2024-04-08 18:44:01 +08:00
public TMP_FontAsset font { get; set; }
2024-04-03 14:38:44 +08:00
protected override void OnInit()
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
base.OnInit();
2024-04-04 23:51:14 +08:00
var types = new List<(Type, UITypeAttribute)>();
AssemblyManager.GetTypesInhertType<UITypeAttribute>(typeof(UIBase), types);
foreach (var (type, uiTypeAttribute) in types)
2024-04-03 14:38:44 +08:00
{
_types.Add(uiTypeAttribute.UIType, type);
}
2024-04-02 18:17:57 +08:00
}
2024-04-03 14:38:44 +08:00
protected override void OnUpdate(float dateTime)
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
base.OnUpdate(dateTime);
foreach (var ui in _uis)
{
ui.Update(dateTime);
}
2024-04-02 18:17:57 +08:00
}
2024-04-03 14:38:44 +08:00
protected override void OnPause()
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
base.OnPause();
foreach (var ui in _uis)
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
ui.Pause();
2024-04-02 18:17:57 +08:00
}
}
2024-04-03 14:38:44 +08:00
protected override void OnResume()
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
base.OnResume();
foreach (var ui in _uis)
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
ui.Resume();
2024-04-02 18:17:57 +08:00
}
}
2024-04-08 18:44:01 +08:00
public IUI CreateUI(UIType uiType, UILayer uiLayer)
2024-04-02 18:17:57 +08:00
{
var gameObject = Game.resourceManager.LoadUIGameObjectSync(uiType.ToString(), uiLayer);
2024-04-03 14:38:44 +08:00
2024-04-04 23:51:14 +08:00
if (!this._types.TryGetValue(uiType, out var type))
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
throw new InvalidOperationException();
2024-04-02 18:17:57 +08:00
}
2024-04-04 23:51:14 +08:00
if (Activator.CreateInstance(type, false) is not UIBase ui)
2024-04-03 14:38:44 +08:00
throw new NullReferenceException();
ui.SetGameObject(gameObject);
2024-04-04 23:51:14 +08:00
ui.Init();
ui.Close();
_uiDic.Add(uiType, ui);
2024-04-08 18:44:01 +08:00
//#if UNITY_EDITOR
// view
2024-04-08 18:44:01 +08:00
var addComponent = gameObject.GetComponent<UIInfo>();
addComponent.SetStart(ui, this.font);
//#endif
2024-04-04 23:51:14 +08:00
2024-04-03 14:38:44 +08:00
return ui;
2024-04-02 18:17:57 +08:00
}
2024-04-03 14:38:44 +08:00
public IUI ShowUI(UIType uiType)
2024-04-02 18:17:57 +08:00
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
_uis.Push(ui);
2024-04-03 14:38:44 +08:00
ui.Open();
2024-04-02 18:17:57 +08:00
return ui;
}
return null;
}
2024-04-03 14:38:44 +08:00
public T GetUI<T>(UIType uiType) where T : UIBase
2024-04-02 18:17:57 +08:00
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
2024-04-03 14:38:44 +08:00
if (ui is not T t)
throw new InvalidCastException();
return t;
2024-04-02 18:17:57 +08:00
}
2024-04-03 14:38:44 +08:00
return default;
2024-04-02 18:17:57 +08:00
}
2024-04-04 23:51:14 +08:00
public bool HideUI(UIType uiType)
2024-04-02 18:17:57 +08:00
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
ui.Close();
2024-04-03 14:38:44 +08:00
return true;
2024-04-02 18:17:57 +08:00
}
2024-04-03 14:38:44 +08:00
return false;
2024-04-02 18:17:57 +08:00
}
public IUI CloseLast()
{
if (_uis.Count > 0)
{
var ui = _uis.Pop();
2024-04-02 18:17:57 +08:00
ui.Close();
return ui;
}
return null;
}
public void CloseAll()
{
while (_uis.Count > 0)
{
var ui = _uis.Pop();
2024-04-02 18:17:57 +08:00
ui.Close();
}
}
}
}