195 lines
5.6 KiB
C#
195 lines
5.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.U2D;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 子 UI 管理器:不继承 MonoBehaviour,适合子场景/模块内局部面板管理
|
|
/// </summary>
|
|
public class SubUIManager
|
|
{
|
|
public UIBase parentUIBase;
|
|
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 GameObject boardCast_Boss;
|
|
|
|
public void ShowBoardBoss(Sprite sprite, string str)
|
|
{
|
|
if (sprite!=null)
|
|
{
|
|
parentUIBase.StartCoroutine(IEShowBoardGongGao(sprite, str));
|
|
|
|
}
|
|
|
|
}
|
|
public float speedGongGao;
|
|
IEnumerator IEShowBoardGongGao(Sprite sprite, string str)
|
|
{
|
|
var obj = GameObject.Instantiate(boardCast_Boss, parent);
|
|
|
|
obj.transform.SetAsLastSibling();
|
|
obj.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = str+"来了";
|
|
obj.transform.GetChild(1).GetComponent<Image>().sprite = sprite;
|
|
yield return new WaitForSeconds(1);
|
|
float a = 1;
|
|
Color c;
|
|
TextMeshProUGUI t = obj.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
|
|
Image i = obj.GetComponent<Image>();
|
|
while (true)
|
|
{
|
|
a -= Time.deltaTime;
|
|
yield return null;
|
|
c = i.color;
|
|
c.a = a;
|
|
i.color = c;
|
|
|
|
c = t.color;
|
|
c.a = a;
|
|
t.color = c;
|
|
if (a < 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
GameObject.Destroy(obj);
|
|
|
|
}
|
|
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,UIBase parentUIBase)
|
|
{
|
|
this.parent = parent;
|
|
this.parentUIBase = parentUIBase;
|
|
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);
|
|
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("当前页面不存在");
|
|
}
|
|
|
|
}
|
|
|
|
private 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);
|
|
instance.transform.SetAsLastSibling(); // 新面板放到最上层
|
|
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;
|
|
//}
|
|
}
|