83 lines
2.9 KiB
C#
83 lines
2.9 KiB
C#
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<UIManager>
|
|
{
|
|
Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
|
|
LinkedList<UIBase> uiLinkedList = new LinkedList<UIBase>();
|
|
|
|
public T ShowUI<T>(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 = AssetLoad.Inst.Load<GameObject>(uiType);
|
|
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
|
openedUIs[uiName] = ui;
|
|
}
|
|
else
|
|
{
|
|
ui = openedUIs[uiName] as T;
|
|
uiLinkedList.Remove(ui);
|
|
}
|
|
Debug.Log($"ShowUI====>name:{uiName} type:{typeof(T).Name}");
|
|
ui.gameObject.SetActive(true);
|
|
ui.OnShow();
|
|
uiLinkedList.AddLast(ui);
|
|
return ui;
|
|
}
|
|
public async UniTask<T> ShowUIAsync<T>(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 AssetLoad.Inst.LoadAsync<GameObject>(uiType);
|
|
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
|
|
openedUIs[uiName] = ui;
|
|
}
|
|
else
|
|
{
|
|
ui = openedUIs[uiName] as T;
|
|
uiLinkedList.Remove(ui);
|
|
}
|
|
Debug.Log($"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];
|
|
ui.OnHide();
|
|
ui.gameObject.SetActive(false);
|
|
uiLinkedList.Remove(ui);
|
|
}
|
|
public void CloseUI(UIBase ui)
|
|
{
|
|
ui.OnHide();
|
|
ui.gameObject.SetActive(false);
|
|
uiLinkedList.Remove(ui);
|
|
}
|
|
T CreateUI<T>(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<T>();
|
|
ui.OnCreate();
|
|
if (parent) ui.SetParent(parent, isFull);
|
|
Debug.Log($"CreateUI====>name:{uiName} type:{typeof(T).Name}");
|
|
return ui;
|
|
}
|
|
}
|
|
} |