162 lines
3.9 KiB
C#
162 lines
3.9 KiB
C#
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; }
|
||
|
||
public override void OnInit(params object[] data)
|
||
{
|
||
base.OnInit(data);
|
||
var types = new List<(Type, UITypeAttribute)>();
|
||
AssemblyManager.GetTypesInhertType<UITypeAttribute>(typeof(UIBase), types);
|
||
foreach (var (type, uiTypeAttribute) in types)
|
||
{
|
||
_types.Add(uiTypeAttribute.UIType, type);
|
||
}
|
||
}
|
||
|
||
public override void OnUpdate(float time, params object[] data)
|
||
{
|
||
base.OnUpdate(time, data);
|
||
foreach (var ui in _uis)
|
||
{
|
||
ui.OnUpdate(time);
|
||
}
|
||
}
|
||
|
||
public override void OnPause(params object[] data)
|
||
{
|
||
base.OnPause(data);
|
||
foreach (var ui in _uis)
|
||
{
|
||
ui.OnPause();
|
||
}
|
||
}
|
||
|
||
public override void OnResume(params object[] data)
|
||
{
|
||
base.OnResume(data);
|
||
foreach (var ui in _uis)
|
||
{
|
||
ui.OnResume();
|
||
}
|
||
}
|
||
|
||
public override void OnDispose(params object[] data)
|
||
{
|
||
base.OnDispose(data);
|
||
DeleteAllUI();
|
||
}
|
||
|
||
private void DeleteAllUI()
|
||
{
|
||
foreach (var uiBase in _uiDic.Values)
|
||
{
|
||
uiBase.OnDispose();
|
||
}
|
||
}
|
||
|
||
|
||
public T CreateUI<T>(UIType uiType, string path, UILayer uiLayer) where T : UIBase
|
||
{
|
||
var gameObject = ResourcesLocalComponent.Instance.LoadUIGameObjectSync(path, uiLayer);
|
||
var uiBase = gameObject.AddComponent<T>();
|
||
uiBase.OnInit();
|
||
uiBase.OnClose();
|
||
_uiDic.Add(uiType, uiBase);
|
||
return uiBase;
|
||
}
|
||
|
||
public void DeleteUI(UIType uiType)
|
||
{
|
||
if (_uiDic.TryGetValue(uiType, out var ui))
|
||
{
|
||
ui.OnDispose();
|
||
}
|
||
else
|
||
{
|
||
throw new NullReferenceException();
|
||
}
|
||
}
|
||
|
||
public IUI ShowUI(UIType uiType)
|
||
{
|
||
if (_uiDic.TryGetValue(uiType, out var ui))
|
||
{
|
||
_uis.Push(ui);
|
||
ui.OnOpen();
|
||
return ui;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public IUI ShowUI(UIBase uiBase)
|
||
{
|
||
_uis.Push(uiBase);
|
||
uiBase.OnOpen();
|
||
return uiBase;
|
||
}
|
||
|
||
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))
|
||
{
|
||
ui.OnClose();
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public bool HideUI(UIBase uiBase)
|
||
{
|
||
uiBase.OnClose();
|
||
// TODO:
|
||
_uis.Pop();
|
||
return true;
|
||
}
|
||
|
||
public IUI CloseLast()
|
||
{
|
||
if (_uis.Count > 0)
|
||
{
|
||
var ui = _uis.Pop();
|
||
ui.OnClose();
|
||
return ui;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public void CloseAll()
|
||
{
|
||
while (_uis.Count > 0)
|
||
{
|
||
var ui = _uis.Pop();
|
||
ui.OnClose();
|
||
}
|
||
}
|
||
}
|
||
} |