using Cysharp.Threading.Tasks; using System; using System.Collections.Generic; using Tuan.GameFramework; using UnityEngine; using YooAsset; namespace Tuan.GameScripts.Main { public class UIManager : Singleton { Dictionary openedUIs = new Dictionary(); LinkedList uiLinkedList = new LinkedList(); 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 = AssetLoader.Inst.Load(uiType); ui = CreateUI(uiPrefab, uiName, parent, isFull); openedUIs[uiName] = ui; } else { ui = openedUIs[uiName] as T; uiLinkedList.Remove(ui); } Debug.Log($"UIManager.ShowUI====>name:{uiName} type:{typeof(T).Name}"); ui.gameObject.SetActive(true); ui.OnShow(); uiLinkedList.AddLast(ui); 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 AssetLoader.Inst.LoadAsync(uiType); ui = CreateUI(uiPrefab, uiName, parent, isFull); openedUIs[uiName] = ui; } else { ui = openedUIs[uiName] as T; uiLinkedList.Remove(ui); } Debug.Log($"UIManager.ShowUIAsync====>name:{uiName} type:{typeof(T).Name}"); ui.gameObject.SetActive(true); ui.OnShow(); uiLinkedList.AddLast(ui); return ui; } public void CloseUI(string uiName) { if (!openedUIs.ContainsKey(uiName)) return; var ui = openedUIs[uiName]; Debug.Log($"UIManager.CloseUI====>name:{uiName} type:{ui}"); ui.OnHide(); ui.gameObject.SetActive(false); uiLinkedList.Remove(ui); } public void CloseUI(UIBase ui) { if (!openedUIs.ContainsKey(ui.gameObject.name)) return; Debug.Log($"UIManager.CloseUI====>name:{ui.gameObject.name} type:{ui}"); ui.OnHide(); ui.gameObject.SetActive(false); uiLinkedList.Remove(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($"UIManager.CreateUI====>name:{uiName} type:{typeof(T).Name}"); return ui; } } }