(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -8,182 +8,174 @@ using Object = UnityEngine.Object;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
/// <summary>
|
||||
/// UI窗口输入控制和管理类
|
||||
/// 负责根据输入显示/隐藏UI,并根据UI状态管理游戏暂停。
|
||||
/// </summary>
|
||||
public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI
|
||||
{
|
||||
// 存储窗口及其激活键的字典
|
||||
public Dictionary<KeyCode, UIBase> UIwindowKeys = new();
|
||||
// 存储没有激活键的窗口列表
|
||||
private List<UIBase> noKeyWindows = new();
|
||||
// 存储场景中所有UIBase的实例
|
||||
private List<UIBase> _allWindows = new List<UIBase>();
|
||||
// 缓存当前可见的窗口
|
||||
private readonly List<UIBase> _visibleWindows = new List<UIBase>();
|
||||
|
||||
// 每帧更新逻辑
|
||||
private bool needUpdate = false;
|
||||
/// <summary>
|
||||
/// 查找并注册场景中所有的UI窗口,包括非激活状态的
|
||||
/// </summary>
|
||||
private void RegisterAllWindows()
|
||||
{
|
||||
_allWindows.Clear();
|
||||
|
||||
// 获取当前活动场景中的所有 GameObject
|
||||
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
|
||||
if (!activeScene.isLoaded)
|
||||
{
|
||||
Debug.LogWarning("当前场景未加载,无法注册窗口!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 遍历场景中的所有根对象,并查找其子对象中的 UIBase
|
||||
foreach (var rootGameObject in activeScene.GetRootGameObjects())
|
||||
{
|
||||
var windows = rootGameObject.GetComponentsInChildren<UIBase>(true);
|
||||
_allWindows.AddRange(windows);
|
||||
}
|
||||
|
||||
// 去重(如果有重复的窗口)
|
||||
_allWindows = _allWindows.Distinct().ToList();
|
||||
|
||||
// 初始化所有窗口为隐藏状态
|
||||
foreach (var window in _allWindows)
|
||||
{
|
||||
if (window != null && window.gameObject != null)
|
||||
{
|
||||
window.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
needUpdate = true;
|
||||
Debug.Log($"窗口数量{_allWindows.Count}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UI逻辑更新循环,需要被外部的某个管理器在Update中调用
|
||||
/// </summary>
|
||||
public void TickUI()
|
||||
{
|
||||
foreach (var kvp in UIwindowKeys)
|
||||
//使用这个是为了让输入独占窗口关闭自己后不会立即激活其他窗口的按键,延迟一帧
|
||||
if (needUpdate)
|
||||
{
|
||||
if (Input.GetKeyDown(kvp.Key))
|
||||
{
|
||||
HandleWindowActivation(kvp.Value);
|
||||
break;
|
||||
}
|
||||
// 更新可见窗口缓存和暂停状态
|
||||
UpdateVisibleWindowsCache();
|
||||
UpdatePauseState();
|
||||
needUpdate = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
UIwindowKeys.Clear();
|
||||
noKeyWindows.Clear();
|
||||
|
||||
var uiInstances = Resources.FindObjectsOfTypeAll<UIBase>();
|
||||
|
||||
foreach (var uiBase in uiInstances)
|
||||
if(_visibleWindows.Any(window => window.isInputOccupied))
|
||||
return;
|
||||
foreach (var window in _allWindows)
|
||||
{
|
||||
var key = uiBase.actionButton;
|
||||
if (key == KeyCode.None)
|
||||
// 检查窗口是否设置了有效的激活按键,并且该按键在本帧被按下
|
||||
if (window.actionButton == KeyCode.None || !Input.GetKeyDown(window.actionButton)) continue;
|
||||
if (window.IsVisible)
|
||||
{
|
||||
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, bool isFunctionCall = false)
|
||||
{
|
||||
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)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (kvp.Value.IsVisible)
|
||||
{
|
||||
if (!wasTargetVisible || isFunctionCall) // 只在目标窗口要打开时才关闭其他窗口
|
||||
// 如果窗口当前是可见的,且未占用输入,则通过按键隐藏它
|
||||
if (!window.isInputOccupied)
|
||||
{
|
||||
kvp.Value.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
anyOtherWindowOpen = true; // 记录是否有其他窗口打开
|
||||
Hide(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (wasTargetVisible)
|
||||
{
|
||||
targetWindow.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
targetWindow.Show();
|
||||
}
|
||||
|
||||
bool currentWindowState = !wasTargetVisible || anyOtherWindowOpen;
|
||||
if (Base.Clock.Instance.Pause != currentWindowState)
|
||||
{
|
||||
Base.Clock.Instance.Pause = currentWindowState;
|
||||
else
|
||||
{
|
||||
// 如果窗口当前是隐藏的,则显示它
|
||||
Show(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模拟按键输入切换窗口
|
||||
/// 公开的显示窗口方法
|
||||
/// </summary>
|
||||
/// <param name="keyCode">要模拟的按键</param>
|
||||
public void SimulateKeyPress(KeyCode keyCode)
|
||||
/// <param name="windowToShow">要显示的窗口</param>
|
||||
public void Show(UIBase windowToShow)
|
||||
{
|
||||
if (UIwindowKeys.TryGetValue(keyCode, out UIBase targetWindow))
|
||||
if (!windowToShow || windowToShow.IsVisible) return;
|
||||
|
||||
// 如果窗口是独占的,隐藏所有其他窗口
|
||||
if (windowToShow.exclusive)
|
||||
{
|
||||
HandleWindowActivation(targetWindow); // 调用内部逻辑处理
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"No window is assigned to the key '{keyCode}'.");
|
||||
List<UIBase> windowsToHide = new List<UIBase>(_visibleWindows);
|
||||
foreach (var visibleWindow in windowsToHide)
|
||||
{
|
||||
Hide(visibleWindow);
|
||||
}
|
||||
}
|
||||
|
||||
// 显示目标窗口并更新缓存与暂停状态
|
||||
windowToShow.Show();
|
||||
var itick=windowToShow as ITickUI;
|
||||
if (itick != null)
|
||||
Base.Clock.AddTickUI(itick);
|
||||
|
||||
needUpdate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开指定的窗口(无论是否有激活键)
|
||||
/// 公开的隐藏窗口方法
|
||||
/// </summary>
|
||||
/// <param name="window">要打开的窗口</param>
|
||||
public void OpenWindow(UIBase window)
|
||||
/// <param name="windowToHide">要隐藏的窗口</param>
|
||||
public void Hide(UIBase windowToHide)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot open the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
if (!windowToHide || !windowToHide.IsVisible) return;
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
// 隐藏目标窗口并更新缓存与暂停状态
|
||||
windowToHide.Hide();
|
||||
needUpdate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭指定的窗口(无论是否有激活键)
|
||||
/// 根据当前所有可见窗口的 needPause 属性来更新游戏时钟的暂停状态
|
||||
/// </summary>
|
||||
/// <param name="window">要关闭的窗口</param>
|
||||
public void CloseWindow(UIBase window)
|
||||
private void UpdatePauseState()
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
bool shouldPause = _visibleWindows.Any(w => w.needPause);
|
||||
if (Base.Clock.Instance.Pause != shouldPause)
|
||||
{
|
||||
Debug.LogWarning("Cannot close the specified window as it is not registered.");
|
||||
return;
|
||||
Base.Clock.Instance.Pause = shouldPause;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换指定窗口的显示状态(无论是否有激活键)
|
||||
/// 更新当前可见窗口的缓存列表
|
||||
/// </summary>
|
||||
/// <param name="window">要切换的窗口</param>
|
||||
public void ToggleWindow(UIBase window)
|
||||
private void UpdateVisibleWindowsCache()
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
_visibleWindows.Clear();
|
||||
foreach (var window in _allWindows)
|
||||
{
|
||||
Debug.LogWarning("Cannot toggle the specified window as it is not registered.");
|
||||
return;
|
||||
if (window.IsVisible)
|
||||
{
|
||||
_visibleWindows.Add(window);
|
||||
}
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在对象销毁时清理事件监听
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在对象启动时初始化
|
||||
/// </summary>
|
||||
protected override void OnStart()
|
||||
{
|
||||
// 注册场景加载事件
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
|
||||
// 初始化时调用一次
|
||||
Init();
|
||||
|
||||
// RegisterAllWindows();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景加载完成后重新初始化
|
||||
/// </summary>
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// 场景加载完成后调用 Init 方法
|
||||
Init();
|
||||
RegisterAllWindows();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user