Framework_YooAsset_HybridCLR/Assets/DemoGame/GameScript/Hotfix/UI/UIManager.cs

162 lines
3.9 KiB
C#
Raw Normal View History

2024-07-19 10:51:48 +08:00
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace ZC
{
internal class UIManager : ManagerBase<UIManager>, IUIManager
{
private Stack<UIBase> _uis = new Stack<UIBase>();
private Dictionary<UIType, UIBase> _uiDic = new Dictionary<UIType, UIBase>();
private Dictionary<UIType, Type> _types = new Dictionary<UIType, Type>();
public TMP_FontAsset font { get; set; }
2024-11-12 16:57:51 +08:00
public override void OnInit(params object[] data)
2024-07-19 10:51:48 +08:00
{
2024-11-12 16:57:51 +08:00
base.OnInit(data);
2024-07-19 10:51:48 +08:00
var types = new List<(Type, UITypeAttribute)>();
AssemblyManager.GetTypesInhertType<UITypeAttribute>(typeof(UIBase), types);
foreach (var (type, uiTypeAttribute) in types)
{
_types.Add(uiTypeAttribute.UIType, type);
}
}
2024-11-12 16:57:51 +08:00
public override void OnUpdate(float time, params object[] data)
2024-07-19 10:51:48 +08:00
{
2024-11-12 16:57:51 +08:00
base.OnUpdate(time, data);
2024-07-19 10:51:48 +08:00
foreach (var ui in _uis)
{
2024-11-12 16:57:51 +08:00
ui.OnUpdate(time);
2024-07-19 10:51:48 +08:00
}
}
2024-11-12 16:57:51 +08:00
public override void OnPause(params object[] data)
2024-07-19 10:51:48 +08:00
{
2024-11-12 16:57:51 +08:00
base.OnPause(data);
2024-07-19 10:51:48 +08:00
foreach (var ui in _uis)
{
2024-11-12 16:57:51 +08:00
ui.OnPause();
2024-07-19 10:51:48 +08:00
}
}
2024-11-12 16:57:51 +08:00
public override void OnResume(params object[] data)
2024-07-19 10:51:48 +08:00
{
2024-11-12 16:57:51 +08:00
base.OnResume(data);
2024-07-19 10:51:48 +08:00
foreach (var ui in _uis)
{
2024-11-12 16:57:51 +08:00
ui.OnResume();
2024-07-19 10:51:48 +08:00
}
}
2024-11-12 17:57:26 +08:00
public override void OnDispose(params object[] data)
{
base.OnDispose(data);
DeleteAllUI();
}
private void DeleteAllUI()
{
foreach (var uiBase in _uiDic.Values)
{
uiBase.OnDispose();
}
}
2024-07-19 10:51:48 +08:00
2024-11-12 16:57:51 +08:00
public T CreateUI<T>(UIType uiType, string path, UILayer uiLayer) where T : UIBase
2024-07-19 10:51:48 +08:00
{
var gameObject = ResourcesLocalComponent.Instance.LoadUIGameObjectSync(path, uiLayer);
2024-11-12 16:57:51 +08:00
var uiBase = gameObject.AddComponent<T>();
uiBase.OnInit();
uiBase.OnClose();
_uiDic.Add(uiType, uiBase);
return uiBase;
}
2024-07-19 10:51:48 +08:00
2024-11-12 16:57:51 +08:00
public void DeleteUI(UIType uiType)
{
if (_uiDic.TryGetValue(uiType, out var ui))
2024-07-19 10:51:48 +08:00
{
2024-11-12 16:57:51 +08:00
ui.OnDispose();
2024-07-19 10:51:48 +08:00
}
2024-11-12 16:57:51 +08:00
else
{
2024-07-19 10:51:48 +08:00
throw new NullReferenceException();
2024-11-12 16:57:51 +08:00
}
2024-07-19 10:51:48 +08:00
}
public IUI ShowUI(UIType uiType)
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
_uis.Push(ui);
2024-11-12 16:57:51 +08:00
ui.OnOpen();
2024-07-19 10:51:48 +08:00
return ui;
}
return null;
}
2024-11-12 16:57:51 +08:00
public IUI ShowUI(UIBase uiBase)
{
_uis.Push(uiBase);
uiBase.OnOpen();
return uiBase;
}
2024-07-19 10:51:48 +08:00
public T GetUI<T>(UIType uiType) where T : UIBase
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
if (ui is not T t)
throw new InvalidCastException();
return t;
}
return default;
}
public bool HideUI(UIType uiType)
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
2024-11-12 16:57:51 +08:00
ui.OnClose();
2024-07-19 10:51:48 +08:00
return true;
}
return false;
}
2024-11-12 16:57:51 +08:00
public bool HideUI(UIBase uiBase)
{
uiBase.OnClose();
// TODO
_uis.Pop();
return true;
}
2024-07-19 10:51:48 +08:00
public IUI CloseLast()
{
if (_uis.Count > 0)
{
var ui = _uis.Pop();
2024-11-12 16:57:51 +08:00
ui.OnClose();
2024-07-19 10:51:48 +08:00
return ui;
}
return null;
}
public void CloseAll()
{
while (_uis.Count > 0)
{
var ui = _uis.Pop();
2024-11-12 16:57:51 +08:00
ui.OnClose();
2024-07-19 10:51:48 +08:00
}
}
}
}