Files
TuanTuan-Engine/Assets/GameScripts/Main/UI/UIManager.cs

85 lines
3.1 KiB
C#
Raw Normal View History

2025-11-06 17:34:33 +08:00
using Cysharp.Threading.Tasks;
2025-11-12 07:04:31 +08:00
using System;
2025-11-06 17:34:33 +08:00
using System.Collections.Generic;
2025-11-12 07:04:31 +08:00
using Tuan.GameFramework;
2025-11-06 17:34:33 +08:00
using UnityEngine;
using YooAsset;
2025-11-12 07:04:31 +08:00
namespace Tuan.GameScripts.Main
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
public class UIManager : Singleton<UIManager>
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
2025-11-12 18:39:09 +08:00
LinkedList<UIBase> uiLinkedList = new LinkedList<UIBase>();
2025-11-12 07:04:31 +08:00
public T ShowUI<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
T ui = null;
string uiType = typeof(T).Name;
if (!openedUIs.ContainsKey(uiName))
{
2025-11-13 08:57:19 +08:00
GameObject uiPrefab = AssetLoader.Inst.Load<GameObject>(uiType);
2025-11-12 07:04:31 +08:00
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
openedUIs[uiName] = ui;
}
else
{
ui = openedUIs[uiName] as T;
2025-11-12 18:39:09 +08:00
uiLinkedList.Remove(ui);
2025-11-12 07:04:31 +08:00
}
2025-11-13 08:57:19 +08:00
Debug.Log($"UIManager.ShowUI====>name:{uiName} type:{typeof(T).Name}");
2025-11-12 18:39:09 +08:00
ui.gameObject.SetActive(true);
2025-11-12 07:04:31 +08:00
ui.OnShow();
2025-11-12 18:39:09 +08:00
uiLinkedList.AddLast(ui);
2025-11-12 07:04:31 +08:00
return ui;
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
public async UniTask<T> ShowUIAsync<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
2025-11-10 16:59:40 +08:00
{
2025-11-12 07:04:31 +08:00
T ui = null;
string uiType = typeof(T).Name;
if (!openedUIs.ContainsKey(uiName))
{
2025-11-13 08:57:19 +08:00
GameObject uiPrefab = await AssetLoader.Inst.LoadAsync<GameObject>(uiType);
2025-11-12 07:04:31 +08:00
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
openedUIs[uiName] = ui;
}
else
{
ui = openedUIs[uiName] as T;
2025-11-12 18:39:09 +08:00
uiLinkedList.Remove(ui);
2025-11-12 07:04:31 +08:00
}
2025-11-13 08:57:19 +08:00
Debug.Log($"UIManager.ShowUIAsync====>name:{uiName} type:{typeof(T).Name}");
2025-11-12 18:39:09 +08:00
ui.gameObject.SetActive(true);
2025-11-12 07:04:31 +08:00
ui.OnShow();
2025-11-12 18:39:09 +08:00
uiLinkedList.AddLast(ui);
2025-11-12 07:04:31 +08:00
return ui;
2025-11-10 16:59:40 +08:00
}
2025-11-12 18:39:09 +08:00
public void CloseUI(string uiName)
{
2025-11-13 08:57:19 +08:00
if (!openedUIs.ContainsKey(uiName)) return;
2025-11-12 18:39:09 +08:00
var ui = openedUIs[uiName];
2025-11-13 08:57:19 +08:00
Debug.Log($"UIManager.CloseUI====>name:{uiName} type:{ui}");
2025-11-12 18:39:09 +08:00
ui.OnHide();
ui.gameObject.SetActive(false);
uiLinkedList.Remove(ui);
}
public void CloseUI(UIBase ui)
{
2025-11-13 08:57:19 +08:00
if (!openedUIs.ContainsKey(ui.gameObject.name)) return;
Debug.Log($"UIManager.CloseUI====>name:{ui.gameObject.name} type:{ui}");
2025-11-12 18:39:09 +08:00
ui.OnHide();
ui.gameObject.SetActive(false);
uiLinkedList.Remove(ui);
}
2025-11-12 07:04:31 +08:00
T CreateUI<T>(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
2025-11-10 16:59:40 +08:00
{
2025-11-12 07:04:31 +08:00
GameObject uiObj = GameObject.Instantiate(uiPrefab);
uiObj.name = uiName;
T ui = uiObj.GetComponent<T>();
ui.OnCreate();
if (parent) ui.SetParent(parent, isFull);
2025-11-13 08:57:19 +08:00
Debug.Log($"UIManager.CreateUI====>name:{uiName} type:{typeof(T).Name}");
2025-11-12 07:04:31 +08:00
return ui;
2025-11-10 16:59:40 +08:00
}
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
}