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

160 lines
5.9 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>();
Dictionary<string, AssetHandle> assetHandles = new Dictionary<string, AssetHandle>();
Stack<UIBase> uiStack = new Stack<UIBase>();
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))
{
GameObject uiPrefab = LoadUI(uiType);
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
openedUIs[uiName] = ui;
uiStack.Push(ui);
}
else
{
ui = openedUIs[uiName] as T;
}
Debug.Log($"ShowUI====>name:{uiName} type:{typeof(T).Name}");
ui.OnShow();
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))
{
GameObject uiPrefab = await LoadUIAsync(uiType);
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
openedUIs[uiName] = ui;
uiStack.Push(ui);
}
else
{
ui = openedUIs[uiName] as T;
}
Debug.Log($"ShowUIAsync====>name:{uiName} type:{typeof(T).Name}");
ui.OnShow();
return ui;
2025-11-10 16:59:40 +08:00
}
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);
Debug.Log($"CreateUI====>name:{uiName} type:{typeof(T).Name}");
return ui;
2025-11-10 16:59:40 +08:00
}
2025-11-12 07:04:31 +08:00
GameObject LoadUI(string uiType)
2025-11-10 16:59:40 +08:00
{
2025-11-12 07:04:31 +08:00
if (!assetHandles.ContainsKey(uiType))
{
AssetHandle uiHandle = YooAssets.LoadAssetSync<GameObject>(uiType);
assetHandles.Add(uiType, uiHandle);
return (GameObject)uiHandle.AssetObject;
}
else
{
AssetHandle uiHandle = assetHandles[uiType];
return (GameObject)uiHandle.AssetObject;
}
2025-11-10 16:59:40 +08:00
}
2025-11-12 07:04:31 +08:00
async UniTask<GameObject> LoadUIAsync(string uiType)
2025-11-10 16:59:40 +08:00
{
2025-11-12 07:04:31 +08:00
if (!assetHandles.ContainsKey(uiType))
{
AssetHandle uiHandle = YooAssets.LoadAssetAsync<GameObject>(uiType);
await uiHandle.ToUniTask();
assetHandles.Add(uiType, uiHandle);
return (GameObject)uiHandle.AssetObject;
}
else
{
AssetHandle uiHandle = assetHandles[uiType];
return (GameObject)uiHandle.AssetObject;
}
2025-11-10 16:59:40 +08:00
}
2025-11-06 17:34:33 +08:00
2025-11-12 07:04:31 +08:00
#region UIBase的
Dictionary<string, GameObject> testOpenedUIs = new Dictionary<string, GameObject>();
Stack<GameObject> testUIStack = new Stack<GameObject>();
public GameObject TestShowUI(string path, string uiName, RectTransform parent = null, bool isFull = false)
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
GameObject ui = null;
if (!testOpenedUIs.ContainsKey(uiName))
{
GameObject uiPrefab = LoadUI(path);
ui = TestCreateUI(uiPrefab, uiName, parent, isFull);
testOpenedUIs[uiName] = ui;
testUIStack.Push(ui);
}
else
{
ui = testOpenedUIs[uiName];
}
Debug.Log($"TestShowUI====>name:{uiName}");
return ui;
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
public async UniTask<GameObject> TestShowUIAsync(string path, string uiName, RectTransform parent = null, bool isFull = false)
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
GameObject ui = null;
if (!testOpenedUIs.ContainsKey(uiName))
{
GameObject uiPrefab = await LoadUIAsync(path);
ui = TestCreateUI(uiPrefab, uiName, parent, isFull);
testOpenedUIs[uiName] = ui;
testUIStack.Push(ui);
}
else
{
ui = testOpenedUIs[uiName];
}
Debug.Log($"TestShowUI====>name:{uiName}");
return ui;
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
GameObject TestCreateUI(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false)
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
GameObject uiObj = GameObject.Instantiate(uiPrefab);
uiObj.name = uiName;
RectTransform ui = uiObj.GetComponent<RectTransform>();
if (parent) TestSetParent(ui, parent, isFull);
Debug.Log($"TestCreateUI====>name:{uiName}");
return uiObj;
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
void TestSetFull(RectTransform rectTransform)
2025-11-06 17:34:33 +08:00
{
2025-11-12 07:04:31 +08:00
rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = Vector2.one;
rectTransform.offsetMin = Vector2.zero;
rectTransform.offsetMax = Vector2.zero;
rectTransform.sizeDelta = Vector2.zero;
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
void TestSetParent(RectTransform ui, RectTransform parent, bool isFull = false)
{
ui.SetParent(parent);
ui.localScale = Vector3.one;
ui.localPosition = Vector3.zero;
ui.localRotation = Quaternion.identity;
if (isFull)
TestSetFull(ui);
}
#endregion
2025-11-06 17:34:33 +08:00
}
2025-11-12 07:04:31 +08:00
}