147 lines
4.3 KiB
C#
147 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
/// <summary>
|
|
/// 子 UI 管理器:不继承 MonoBehaviour,适合子场景/模块内局部面板管理
|
|
/// </summary>
|
|
public class SubUIManager
|
|
{
|
|
public UIBase subUIBaseParent;
|
|
private readonly Transform parent;
|
|
private readonly Dictionary<UIPanel, SubUIBase> panels = new Dictionary<UIPanel, SubUIBase>();
|
|
private readonly Dictionary<UIPanel, string> addressMap = new Dictionary<UIPanel, string>();
|
|
public T Get<T>(UIPanel type) where T : UIBase
|
|
{
|
|
return panels.TryGetValue(type, out var panel) ? panel as T : null;
|
|
}
|
|
public SubUIManager(Transform parent, UIPanelAddressConfig uIPanelAddressConfig)
|
|
{
|
|
this.parent = parent;
|
|
foreach (var entry in uIPanelAddressConfig.panels)
|
|
{
|
|
if (!addressMap.ContainsKey(entry.type))
|
|
addressMap.Add(entry.type, entry.address);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 这里使用C#的异步所以操作建议在onshown中进行要不然外面的函数要加异步
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <param name="args"></param>
|
|
/// <param name="onShown"></param>
|
|
public async void ShowAsync(UIPanel type, object args = null)
|
|
{
|
|
if (addressMap.ContainsKey(type)) {
|
|
|
|
UIBase uIBase = await LoadAndShowAsync(type, addressMap[type], args);
|
|
subUIBaseParent = uIBase;
|
|
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("当前页面不存在");
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<SubUIBase> LoadAndShowAsync(UIPanel type, string address, object args = null)
|
|
{
|
|
if (panels.TryGetValue(type, out var existing) && existing != null)
|
|
{
|
|
existing.Show(args);
|
|
return existing;
|
|
}
|
|
|
|
if (!addressMap.TryGetValue(type, out var obj) || obj == null)
|
|
{
|
|
Debug.LogError($"[SubUI] No prefab for {type}");
|
|
return null;
|
|
}
|
|
|
|
AsyncOperationHandle<GameObject> handle = Addressables.LoadAssetAsync<GameObject>(address);
|
|
await handle.Task; // 等待异步完成
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
var prefab = handle.Result;
|
|
var instance = Object.Instantiate(prefab, parent).GetComponent<SubUIBase>();
|
|
instance.uIManager = this;
|
|
instance.name = $"Sub_{type}_Panel";
|
|
instance.Init(type);
|
|
panels[type] = instance;
|
|
//return instance;
|
|
|
|
|
|
//var instance = GetOrCreate(type, prefab);
|
|
instance.Show(args);
|
|
return instance;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Failed to load UI prefab at {address}");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void Hide(UIPanel type)
|
|
{
|
|
if (panels.TryGetValue(type, out var panel) && panel != null)
|
|
panel.Hide();
|
|
}
|
|
public void Hide(SubUIBase type)
|
|
{
|
|
|
|
foreach (var panel in panels.Values)
|
|
{
|
|
if (panel== type)
|
|
{
|
|
panel.Hide();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
public void Close(UIPanel type)
|
|
{
|
|
if (panels.TryGetValue(type, out var panel) && panel != null)
|
|
{
|
|
panel.Hide();
|
|
Object.Destroy(panel.gameObject);
|
|
}
|
|
panels.Remove(type);
|
|
}
|
|
|
|
public void CloseAll()
|
|
{
|
|
foreach (var kv in panels)
|
|
{
|
|
if (kv.Value != null)
|
|
{
|
|
kv.Value.Hide();
|
|
Object.Destroy(kv.Value.gameObject);
|
|
}
|
|
}
|
|
panels.Clear();
|
|
}
|
|
|
|
//private UIBase GetOrCreate(UIPanel type,GameObject prefab)
|
|
//{
|
|
// if (panels.TryGetValue(type, out var existing) && existing != null)
|
|
// return existing;
|
|
|
|
// if (!addressMap.TryGetValue(type, out var obj) || prefab == null)
|
|
// {
|
|
// Debug.LogError($"[SubUI] No prefab for {type}");
|
|
// return null;
|
|
// }
|
|
|
|
// var instance = Object.Instantiate(prefab, parent).GetComponent<SubUIBase>();
|
|
// instance.name = $"Sub_{type}_Panel";
|
|
// instance.Init(type);
|
|
// panels[type] = instance;
|
|
// return instance;
|
|
//}
|
|
}
|