using Cysharp.Threading.Tasks; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using YooAsset; public class UIManager : Singleton { public Dictionary openedUIs = new Dictionary(); Stack uiStack = new Stack(); public T ShowUI(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase { T ui = null; string uiType = typeof(T).Name; if (!openedUIs.ContainsKey(uiName)) { GameObject uiPrefab = LoadUI(uiType); ui = CreateUI(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; } public async UniTask ShowUIAsync(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase { T ui = null; string uiType = typeof(T).Name; if (!openedUIs.ContainsKey(uiName)) { GameObject uiPrefab = await LoadUIAsync(uiType); ui = CreateUI(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; } T CreateUI(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase { GameObject uiObj = GameObject.Instantiate(uiPrefab); uiObj.name = uiName; T ui = uiObj.GetComponent(); ui.OnCreate(); if (parent) ui.SetParent(parent, isFull); Debug.Log($"CreateUI====>name:{uiName} type:{typeof(T).Name}"); return ui; } GameObject LoadUI(string uiType) { AssetHandle uiHandle = YooAssets.LoadAssetSync(uiType); return (GameObject)uiHandle.AssetObject; } async UniTask LoadUIAsync(string uiType) { AssetHandle uiHandle = YooAssets.LoadAssetAsync(uiType); await uiHandle.ToUniTask(); return (GameObject)uiHandle.AssetObject; } #region 用于测试没挂载UIBase的 public Dictionary testOpenedUIs = new Dictionary(); Stack testUIStack = new Stack(); public GameObject TestShowUI(string path, string uiName, RectTransform parent = null, bool isFull = false) { 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; } public async UniTask< GameObject> TestShowUIAsync(string path, string uiName, RectTransform parent = null, bool isFull = false) { 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; } GameObject TestCreateUI(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false) { GameObject uiObj = GameObject.Instantiate(uiPrefab); uiObj.name = uiName; RectTransform ui = uiObj.GetComponent(); if (parent) TestSetParent(ui, parent, isFull); Debug.Log($"TestCreateUI====>name:{uiName}"); return uiObj; } void TestSetFull(RectTransform rectTransform) { rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.one; rectTransform.offsetMin = Vector2.zero; rectTransform.offsetMax = Vector2.zero; rectTransform.sizeDelta = Vector2.zero; } 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 }