Init
This commit is contained in:
85
Assets/GameScripts/Main/UI/UIManager.cs
Normal file
85
Assets/GameScripts/Main/UI/UIManager.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
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 = AssetLoader.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($"UIManager.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 AssetLoader.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($"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<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($"UIManager.CreateUI====>name:{uiName} type:{typeof(T).Name}");
|
||||
return ui;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user