196 lines
4.6 KiB
C#
196 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace HK
|
|
{
|
|
enum LayerType
|
|
{
|
|
High,
|
|
Middle,
|
|
Low,
|
|
}
|
|
|
|
internal class UIManager : ManagerBase<UIManager>, IUIManager, IReference
|
|
{
|
|
private Stack<UIBase> _uis = new Stack<UIBase>();
|
|
private Dictionary<string, GameObject> _uiTemp = new Dictionary<string, GameObject>();
|
|
[SerializeField] Transform canvas;
|
|
|
|
public void Initialize()
|
|
{
|
|
canvas ??= GameObject.Find("UI").transform;
|
|
}
|
|
|
|
public UIManager()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
CloseAll();
|
|
}
|
|
|
|
public void LoadUI(string uiName, string assetPath)
|
|
{
|
|
if (!_uiTemp.ContainsKey(uiName))
|
|
{
|
|
var gameObject = ResourcesManager.Instance.Load<GameObject>(assetPath);
|
|
_uiTemp.Add(uiName, gameObject);
|
|
}
|
|
}
|
|
|
|
public IUI CreateUI(string uiName)
|
|
{
|
|
var o = _uiTemp[uiName];
|
|
var go = GameObject.Instantiate(o, canvas);
|
|
var uiBase = go.GetComponent<UIBase>();
|
|
uiBase.OnClose();
|
|
return uiBase;
|
|
}
|
|
|
|
public IUI ShowUI(string uiName)
|
|
{
|
|
if (!_uiTemp.ContainsKey(uiName))
|
|
{
|
|
throw new NullReferenceException($"UI \"{uiName}\" not found");
|
|
}
|
|
|
|
var o = _uiTemp[uiName];
|
|
var go = GameObject.Instantiate(o, canvas);
|
|
var uiBase = go.GetComponent<UIBase>();
|
|
_uis.Push(uiBase);
|
|
uiBase.OnOpen();
|
|
return uiBase;
|
|
}
|
|
|
|
public IUI ShowUIOnly(string uiName)
|
|
{
|
|
if (!_uiTemp.ContainsKey(uiName))
|
|
{
|
|
throw new NullReferenceException($"UI \"{uiName}\" not found");
|
|
}
|
|
|
|
if (_uis.TryPeek(out var ui))
|
|
{
|
|
Debug.Log(ui.name);
|
|
ui.OnClose();
|
|
}
|
|
|
|
var o = _uiTemp[uiName];
|
|
var go = GameObject.Instantiate(o, canvas);
|
|
var uiBase = go.GetComponent<UIBase>();
|
|
_uis.Push(uiBase);
|
|
uiBase.OnOpen();
|
|
return uiBase;
|
|
}
|
|
|
|
public T GetUI<T>(string uiName) where T : UIBase
|
|
{
|
|
foreach (var uiBase in _uis)
|
|
{
|
|
if (uiBase is not T t)
|
|
throw new InvalidCastException();
|
|
return t;
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
public bool HideUI(string uiName)
|
|
{
|
|
var array = _uis.ToList();
|
|
foreach (var uiBase in array)
|
|
{
|
|
if (uiBase.name == uiName)
|
|
{
|
|
uiBase.OnClose();
|
|
uiBase.OnDispose();
|
|
array.Remove(uiBase);
|
|
break;
|
|
}
|
|
}
|
|
|
|
_uis.Clear();
|
|
for (var i = array.Count - 1; i >= 0; i--)
|
|
{
|
|
_uis.Push(array[i]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool HideUI(UIBase ui)
|
|
{
|
|
return HideUI(ui.name);
|
|
}
|
|
|
|
public IUI ReturnPreviousUI()
|
|
{
|
|
if (_uis.Count > 0)
|
|
{
|
|
var ui = _uis.Pop();
|
|
ui.OnClose();
|
|
ui.OnDispose();
|
|
|
|
ui = _uis.Pop();
|
|
ui.OnOpen();
|
|
return ui;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void Log()
|
|
{
|
|
foreach (var uiBase in _uis)
|
|
{
|
|
Debug.Log(uiBase.name);
|
|
}
|
|
}
|
|
|
|
public IUI BackLast()
|
|
{
|
|
if (_uis.Count > 1)
|
|
{
|
|
var ui = _uis.Pop();
|
|
ui.OnClose();
|
|
ui.OnDispose();
|
|
_uis.TryPeek(out ui);
|
|
ui.OnOpen();
|
|
return ui;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void OnPause(params object[] data)
|
|
{
|
|
foreach (var ui in _uis)
|
|
{
|
|
ui.OnPause();
|
|
}
|
|
}
|
|
|
|
public void OnResume(params object[] data)
|
|
{
|
|
foreach (var ui in _uis)
|
|
{
|
|
ui.OnResume();
|
|
}
|
|
}
|
|
|
|
public void CloseAll()
|
|
{
|
|
while (_uis.Count > 0)
|
|
{
|
|
var ui = _uis.Pop();
|
|
ui.OnClose();
|
|
ui.OnDispose();
|
|
}
|
|
}
|
|
}
|
|
} |