(client):feat:添加对列表定义读取的支持,ui管理器将对没有键的窗口也进行统一管理
This commit is contained in:
@ -7,23 +7,17 @@ namespace Base
|
||||
{
|
||||
public interface ITick
|
||||
{
|
||||
public void Tick()
|
||||
{
|
||||
}
|
||||
public void Tick();
|
||||
}
|
||||
|
||||
public interface ITickPhysics
|
||||
{
|
||||
public void TickPhysics()
|
||||
{
|
||||
}
|
||||
public void TickPhysics();
|
||||
}
|
||||
|
||||
public interface ITickUI
|
||||
{
|
||||
public void TickUI()
|
||||
{
|
||||
}
|
||||
public void TickUI();
|
||||
}
|
||||
|
||||
public class Clock : MonoSingleton<Clock>
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
@ -7,11 +8,15 @@ using Object = UnityEngine.Object;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
public class UIInputControl:Utils.MonoSingleton<UIInputControl>
|
||||
public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI
|
||||
{
|
||||
// 存储窗口及其激活键的字典
|
||||
public Dictionary<KeyCode, UIBase> UIwindowKeys = new();
|
||||
|
||||
private void Update()
|
||||
// 存储没有激活键的窗口列表
|
||||
private List<UIBase> noKeyWindows = new();
|
||||
|
||||
// 每帧更新逻辑
|
||||
public void TickUI()
|
||||
{
|
||||
foreach (var kvp in UIwindowKeys)
|
||||
{
|
||||
@ -22,60 +27,147 @@ namespace Base
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void Init()
|
||||
{
|
||||
// 查找所有继承自 UIBase 的实例
|
||||
UIBase[] uiInstances = Object.FindObjectsByType<UIBase>(FindObjectsSortMode.None);
|
||||
UIwindowKeys.Clear();
|
||||
noKeyWindows.Clear();
|
||||
|
||||
var uiInstances = Resources.FindObjectsOfTypeAll<UIBase>();
|
||||
|
||||
foreach (var uiBase in uiInstances)
|
||||
{
|
||||
var key = uiBase.actionButton;
|
||||
|
||||
// 忽略 None 按键
|
||||
if (key == KeyCode.None)
|
||||
{
|
||||
noKeyWindows.Add(uiBase);
|
||||
uiBase.Hide();
|
||||
continue;
|
||||
|
||||
// 检查按键是否重复
|
||||
}
|
||||
if (UIwindowKeys.ContainsKey(key))
|
||||
{
|
||||
Debug.LogWarning($"Key '{key}' is already assigned to another window. Skipping...");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 添加到字典
|
||||
UIwindowKeys[key] = uiBase;
|
||||
uiBase.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleWindowActivation(UIBase targetWindow)
|
||||
|
||||
private void HandleWindowActivation(UIBase targetWindow, bool isFunctionCall = false)
|
||||
{
|
||||
// 遍历所有窗口,关闭非目标窗口
|
||||
foreach (var kvp in UIwindowKeys)
|
||||
bool wasTargetVisible = targetWindow.IsVisible;
|
||||
bool anyOtherWindowOpen = false;
|
||||
|
||||
// 遍历所有窗口(包括有键和无键窗口)
|
||||
foreach (var kvp in UIwindowKeys.Concat(noKeyWindows.Select(w => new KeyValuePair<KeyCode, UIBase>(KeyCode.None, w))))
|
||||
{
|
||||
if (kvp.Value != targetWindow && kvp.Value.IsVisible)
|
||||
if (kvp.Value == targetWindow)
|
||||
{
|
||||
kvp.Value.Hide();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (kvp.Value.IsVisible)
|
||||
{
|
||||
if (!wasTargetVisible || isFunctionCall) // 只在目标窗口要打开时才关闭其他窗口
|
||||
{
|
||||
kvp.Value.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
anyOtherWindowOpen = true; // 记录是否有其他窗口打开
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 切换目标窗口的显示状态
|
||||
if (targetWindow.IsVisible)
|
||||
if (wasTargetVisible)
|
||||
{
|
||||
targetWindow.Hide(); // 如果已经打开,则关闭
|
||||
targetWindow.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
targetWindow.Show(); // 如果未打开,则打开
|
||||
targetWindow.Show();
|
||||
}
|
||||
|
||||
bool currentWindowState = !wasTargetVisible || anyOtherWindowOpen;
|
||||
if (Base.Clock.Instance.Pause != currentWindowState)
|
||||
{
|
||||
Base.Clock.Instance.Pause = currentWindowState;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模拟按键输入切换窗口
|
||||
/// </summary>
|
||||
/// <param name="keyCode">要模拟的按键</param>
|
||||
public void SimulateKeyPress(KeyCode keyCode)
|
||||
{
|
||||
if (UIwindowKeys.TryGetValue(keyCode, out UIBase targetWindow))
|
||||
{
|
||||
HandleWindowActivation(targetWindow); // 调用内部逻辑处理
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"No window is assigned to the key '{keyCode}'.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开指定的窗口(无论是否有激活键)
|
||||
/// </summary>
|
||||
/// <param name="window">要打开的窗口</param>
|
||||
public void OpenWindow(UIBase window)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot open the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭指定的窗口(无论是否有激活键)
|
||||
/// </summary>
|
||||
/// <param name="window">要关闭的窗口</param>
|
||||
public void CloseWindow(UIBase window)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot close the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换指定窗口的显示状态(无论是否有激活键)
|
||||
/// </summary>
|
||||
/// <param name="window">要切换的窗口</param>
|
||||
public void ToggleWindow(UIBase window)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot toggle the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在对象销毁时清理事件监听
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
// 移除事件监听
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在对象启动时初始化
|
||||
/// </summary>
|
||||
protected override void OnStart()
|
||||
{
|
||||
// 注册场景加载事件
|
||||
@ -84,6 +176,10 @@ namespace Base
|
||||
// 初始化时调用一次
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景加载完成后重新初始化
|
||||
/// </summary>
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// 场景加载完成后调用 Init 方法
|
||||
|
Reference in New Issue
Block a user