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

150 lines
3.6 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
using System.Data;
2024-04-03 14:38:44 +08:00
using System.Reflection;
2024-04-02 18:17:57 +08:00
using UnityEngine;
namespace Game
{
public class UIManager : ManagerBase, IUIManager
{
2024-04-03 14:38:44 +08:00
private static UIManager instance;
2024-04-02 18:17:57 +08:00
2024-04-03 14:38:44 +08:00
public static UIManager Instance
2024-04-02 18:17:57 +08:00
{
2024-04-03 14:38:44 +08:00
get
{
if (instance == null)
{
instance = new UIManager();
}
return instance;
}
2024-04-02 18:17:57 +08:00
}
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();
foreach (var type in this.GetType().Assembly.GetTypes())
{
if (!typeof(UIBase).IsAssignableFrom(type))
continue;
var uiTypeAttribute = type.GetCustomAttribute<UITypeAttribute>();
if(uiTypeAttribute==null)
continue;
_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-03 14:38:44 +08:00
var gameObject = ResourceManager.Instance.LoadUIGameObjectSync(uiType.ToString());
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-03 14:38:44 +08:00
if (Activator.CreateInstance(type,false) is not UIBase ui)
throw new NullReferenceException();
ui.SetGameObject(gameObject);
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-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-03 14:38:44 +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();
}
}
}
}