138 lines
3.3 KiB
C#
138 lines
3.3 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()
|
|
{
|
|
base.OnInit();
|
|
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)
|
|
{
|
|
base.OnUpdate(time);
|
|
foreach (var ui in _uis)
|
|
{
|
|
ui.Update(time);
|
|
}
|
|
}
|
|
|
|
public override void OnPause()
|
|
{
|
|
base.OnPause();
|
|
foreach (var ui in _uis)
|
|
{
|
|
ui.Pause();
|
|
}
|
|
}
|
|
|
|
public override void OnResume()
|
|
{
|
|
base.OnResume();
|
|
foreach (var ui in _uis)
|
|
{
|
|
ui.Resume();
|
|
}
|
|
}
|
|
|
|
|
|
public IUI CreateUI(UIType uiType, string path, UILayer uiLayer)
|
|
{
|
|
var gameObject = ResourcesLocalComponent.Instance.LoadUIGameObjectSync(path, uiLayer);
|
|
|
|
if (!this._types.TryGetValue(uiType, out var type))
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
if (Activator.CreateInstance(type, false) is not UIBase ui)
|
|
throw new NullReferenceException();
|
|
ui.SetGameObject(gameObject);
|
|
|
|
ui.Init();
|
|
ui.Close();
|
|
_uiDic.Add(uiType, ui);
|
|
|
|
//#if UNITY_EDITOR
|
|
// view
|
|
// var addComponent = gameObject.GetComponent<UIInfo>();
|
|
// addComponent.SetStart(ui, this.font);
|
|
//#endif
|
|
|
|
return ui;
|
|
}
|
|
|
|
public IUI ShowUI(UIType uiType)
|
|
{
|
|
if (_uiDic.TryGetValue(uiType, out var ui))
|
|
{
|
|
_uis.Push(ui);
|
|
ui.Open();
|
|
return ui;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
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.Close();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public IUI CloseLast()
|
|
{
|
|
if (_uis.Count > 0)
|
|
{
|
|
var ui = _uis.Pop();
|
|
ui.Close();
|
|
return ui;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void CloseAll()
|
|
{
|
|
while (_uis.Count > 0)
|
|
{
|
|
var ui = _uis.Pop();
|
|
ui.Close();
|
|
}
|
|
}
|
|
|
|
}
|
|
} |