(client)feat:添加UI管理器,统一管理继承自UIBase的UI并保证显示唯一
This commit is contained in:
@ -208,6 +208,8 @@ GameObject:
|
||||
m_Component:
|
||||
- component: {fileID: 603423468}
|
||||
- component: {fileID: 603423467}
|
||||
- component: {fileID: 603423470}
|
||||
- component: {fileID: 603423469}
|
||||
m_Layer: 0
|
||||
m_Name: Launcher
|
||||
m_TagString: Untagged
|
||||
@ -242,6 +244,32 @@ Transform:
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &603423469
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 603423466}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: cd0367766d04d8c4db80bcfe0202e619, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
isGlobal: 1
|
||||
--- !u!114 &603423470
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 603423466}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 66ec6c65c80a43e799c1cb67fc9f7691, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
isGlobal: 1
|
||||
--- !u!1 &1236970682
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -1,10 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
public class UIInputControl:Utils.MonoSingleton<UIInputControl>
|
||||
{
|
||||
public Dictionary<KeyCode, UIBase> UIwindowKeys = new();
|
||||
|
||||
private void Update()
|
||||
{
|
||||
foreach (var kvp in UIwindowKeys)
|
||||
{
|
||||
if (Input.GetKeyDown(kvp.Key))
|
||||
{
|
||||
HandleWindowActivation(kvp.Value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void Init()
|
||||
{
|
||||
// 查找所有继承自 UIBase 的实例
|
||||
UIBase[] uiInstances = Object.FindObjectsByType<UIBase>(FindObjectsSortMode.None);
|
||||
|
||||
foreach (var uiBase in uiInstances)
|
||||
{
|
||||
var key = uiBase.actionButton;
|
||||
|
||||
// 忽略 None 按键
|
||||
if (key == KeyCode.None)
|
||||
continue;
|
||||
|
||||
// 检查按键是否重复
|
||||
if (UIwindowKeys.ContainsKey(key))
|
||||
{
|
||||
Debug.LogWarning($"Key '{key}' is already assigned to another window. Skipping...");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 添加到字典
|
||||
UIwindowKeys[key] = uiBase;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleWindowActivation(UIBase targetWindow)
|
||||
{
|
||||
// 遍历所有窗口,关闭非目标窗口
|
||||
foreach (var kvp in UIwindowKeys)
|
||||
{
|
||||
if (kvp.Value != targetWindow && kvp.Value.IsVisible)
|
||||
{
|
||||
kvp.Value.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
// 切换目标窗口的显示状态
|
||||
if (targetWindow.IsVisible)
|
||||
{
|
||||
targetWindow.Hide(); // 如果已经打开,则关闭
|
||||
}
|
||||
else
|
||||
{
|
||||
targetWindow.Show(); // 如果未打开,则打开
|
||||
}
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
// 移除事件监听
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
|
||||
// 注册场景加载事件
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
|
||||
// 初始化时调用一次
|
||||
Init();
|
||||
}
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// 场景加载完成后调用 Init 方法
|
||||
Init();
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ public class Program : MonoBehaviour
|
||||
{
|
||||
UnityLogger.Init();
|
||||
Managers.DefineManager.Instance.Init();
|
||||
Base.UIInputControl.Instance.isGlobal = true;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
|
@ -5,13 +5,18 @@ using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class DevMenuUI : MonoBehaviour
|
||||
public class DevMenuUI : UIBase
|
||||
{
|
||||
public GameObject menuContent;
|
||||
|
||||
public Prefab.TextPrefab textTemplate;
|
||||
public Prefab.ButtonPrefab buttonTemplate;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
actionButton = KeyCode.F1;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Init();
|
||||
@ -19,7 +24,6 @@ namespace UI
|
||||
|
||||
void Init()
|
||||
{
|
||||
Managers.DefineManager.Instance.Init();
|
||||
InitEvent();
|
||||
InitCharacter();
|
||||
}
|
||||
@ -32,6 +36,7 @@ namespace UI
|
||||
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
// button.text.text = i.ToString();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void InitCharacter()
|
||||
|
15
Client/Assets/Scripts/UI/UIBase.cs
Normal file
15
Client/Assets/Scripts/UI/UIBase.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public abstract class UIBase:MonoBehaviour
|
||||
{
|
||||
public KeyCode actionButton = KeyCode.None;
|
||||
// 显示或隐藏窗口
|
||||
public virtual void Show() { gameObject.SetActive(true); }
|
||||
public virtual void Hide() { gameObject.SetActive(false); }
|
||||
|
||||
// 判断是否可见
|
||||
public bool IsVisible => gameObject.activeInHierarchy;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/UIBase.cs.meta
Normal file
3
Client/Assets/Scripts/UI/UIBase.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 293a60af40144aafabdf65f49f5c6e44
|
||||
timeCreated: 1752926575
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user