Frame/Assets/Scripts/UI/UIManager.cs

129 lines
3.1 KiB
C#
Raw Normal View History

2024-04-03 14:38:44 +08:00
using System;
using System.Collections.Generic;
2024-04-02 18:17:57 +08:00
namespace Game
{
public class UIManager : ManagerBase, IUIManager
{
2024-04-03 14:38:44 +08:00
private Queue<UIBase> _uis = new Queue<UIBase>();
private Dictionary<UIType, UIBase> _uiDic = new Dictionary<UIType, UIBase>();
private Dictionary<UIType, Type> _types = new Dictionary<UIType, Type>();
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-03 14:38:44 +08:00
public IUI CreateUI(UIType uiType)
2024-04-02 18:17:57 +08:00
{
2024-04-04 23:51:14 +08:00
var gameObject = Game.resourceManager.LoadUIGameObjectSync(uiType.ToString());
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
2024-04-03 17:46:56 +08:00
#if UNITY_EDITOR
2024-04-03 14:38:44 +08:00
gameObject.AddComponent<UIInfo>().ui = ui;
2024-04-03 17:46:56 +08:00
#endif
2024-04-04 23:51:14 +08:00
2024-04-03 14:38:44 +08:00
ui.Init();
_uiDic.Add(uiType, ui);
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))
{
2024-04-03 14:38:44 +08:00
_uis.Enqueue(ui);
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.Dequeue();
ui.Close();
return ui;
}
return null;
}
public void CloseAll()
{
while (_uis.Count > 0)
{
var ui = _uis.Dequeue();
ui.Close();
}
}
}
}