Introduce UiManager/MainHUD prefab flow, strengthen GameNet/WsClient, and add AIServer maintenance endpoint support. Co-authored-by: Cursor <cursoragent@cursor.com>
79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace HotUpdate.UI
|
|
{
|
|
/// <summary>
|
|
/// 竖屏 UI 页面栈:加载 Prefab、打开/关闭、按名查找。
|
|
/// </summary>
|
|
public sealed class UiManager : MonoBehaviour
|
|
{
|
|
public static UiManager Instance { get; private set; }
|
|
|
|
readonly Dictionary<string, GameObject> _opened = new Dictionary<string, GameObject>(StringComparer.Ordinal);
|
|
Transform _root;
|
|
UiPrefabLoader _loader;
|
|
|
|
public void Init(Transform root, UiPrefabLoader loader)
|
|
{
|
|
Instance = this;
|
|
_root = root;
|
|
_loader = loader ?? new UiPrefabLoader();
|
|
}
|
|
|
|
public GameObject Open(string prefabName, int siblingIndex = -1)
|
|
{
|
|
if (string.IsNullOrEmpty(prefabName)) return null;
|
|
if (_opened.TryGetValue(prefabName, out var existing) && existing != null)
|
|
{
|
|
existing.SetActive(true);
|
|
return existing;
|
|
}
|
|
|
|
var prefab = _loader.Load(prefabName);
|
|
if (prefab == null)
|
|
{
|
|
Debug.LogError($"[UiManager] prefab not found: {prefabName}");
|
|
return null;
|
|
}
|
|
|
|
var go = Instantiate(prefab, _root, false);
|
|
go.name = prefabName;
|
|
if (siblingIndex >= 0)
|
|
go.transform.SetSiblingIndex(siblingIndex);
|
|
_opened[prefabName] = go;
|
|
return go;
|
|
}
|
|
|
|
public T Open<T>(string prefabName) where T : Component
|
|
{
|
|
var go = Open(prefabName);
|
|
return go != null ? go.GetComponentInChildren<T>(true) : null;
|
|
}
|
|
|
|
public void Close(string prefabName, bool destroy = false)
|
|
{
|
|
if (!_opened.TryGetValue(prefabName, out var go) || go == null)
|
|
return;
|
|
if (destroy)
|
|
{
|
|
Destroy(go);
|
|
_opened.Remove(prefabName);
|
|
}
|
|
else go.SetActive(false);
|
|
}
|
|
|
|
public GameObject Get(string prefabName)
|
|
{
|
|
_opened.TryGetValue(prefabName, out var go);
|
|
return go;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (Instance == this) Instance = null;
|
|
}
|
|
}
|
|
}
|