From d17eaaaa8b9fbdc64b1e1b1cf48c7b9d79957fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Tue, 9 Dec 2025 20:30:11 +0800 Subject: [PATCH] opt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改命名空间 --- .../UX/Hotkey/HotkeyBindComponentInspector.cs | 4 +- Editor/UX/Hotkey/UXHotkeyButtonEditor.cs | 9 +- .../Controller/ControllerStateBase.cs | 4 +- .../Property/GameObjectPropertyState.cs | 2 +- .../UXComponent/Controller/UXController.cs | 2 +- .../Controller/UXControllerStateRecorder.cs | 5 +- Runtime/UXComponent/Group/UXGroup.cs | 230 +++++++++--------- .../UXComponent/Hotkey/HotkeyBindComponent.cs | 2 +- Runtime/UXComponent/Hotkey/UXHotkeyButton.cs | 8 +- .../Hotkey/UXHotkeyRegisterManager.cs | 205 ++++++++-------- Runtime/UXComponent/UX/UXDraggable.cs | 2 +- 11 files changed, 240 insertions(+), 233 deletions(-) diff --git a/Editor/UX/Hotkey/HotkeyBindComponentInspector.cs b/Editor/UX/Hotkey/HotkeyBindComponentInspector.cs index d211115..4417830 100644 --- a/Editor/UX/Hotkey/HotkeyBindComponentInspector.cs +++ b/Editor/UX/Hotkey/HotkeyBindComponentInspector.cs @@ -2,13 +2,11 @@ using System.Collections.Generic; using System.Reflection; using AlicizaX.Editor; -using AlicizaX.UI.Extension; -using AlicizaX.UI.Extension.UXComponent.Hotkey; using AlicizaX.UI.Runtime; using UnityEditor; using UnityEngine; -namespace UnityEngine.UI +namespace AlicizaX.UI { [CustomEditor(typeof(HotkeyBindComponent))] public class HotkeyBindComponentInspector : GameFrameworkInspector diff --git a/Editor/UX/Hotkey/UXHotkeyButtonEditor.cs b/Editor/UX/Hotkey/UXHotkeyButtonEditor.cs index f10636a..4b686a4 100644 --- a/Editor/UX/Hotkey/UXHotkeyButtonEditor.cs +++ b/Editor/UX/Hotkey/UXHotkeyButtonEditor.cs @@ -1,8 +1,9 @@ -using AlicizaX.Editor; -using AlicizaX.UI.Extension; +#if INPUTSYSTEM_SUPPORT +using AlicizaX.UI; using UnityEditor; +using UnityEngine; -namespace UnityEngine.UI +namespace AlicizaX.UI { [CanEditMultipleObjects] [CustomEditor(typeof(UXHotkeyButton), true)] @@ -29,3 +30,5 @@ namespace UnityEngine.UI } } } + +#endif diff --git a/Runtime/UXComponent/Controller/ControllerStateBase.cs b/Runtime/UXComponent/Controller/ControllerStateBase.cs index 690fb85..519272d 100644 --- a/Runtime/UXComponent/Controller/ControllerStateBase.cs +++ b/Runtime/UXComponent/Controller/ControllerStateBase.cs @@ -1,9 +1,7 @@ -// Assets/Scripts/AlicizaX/UI/IControllerState.cs - using System; using UnityEngine; -namespace AlicizaX.UI.Runtime +namespace AlicizaX.UI { public abstract class ControllerStateBase { diff --git a/Runtime/UXComponent/Controller/Property/GameObjectPropertyState.cs b/Runtime/UXComponent/Controller/Property/GameObjectPropertyState.cs index 003fb8a..ae72aa8 100644 --- a/Runtime/UXComponent/Controller/Property/GameObjectPropertyState.cs +++ b/Runtime/UXComponent/Controller/Property/GameObjectPropertyState.cs @@ -3,7 +3,7 @@ using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; -namespace AlicizaX.UI.Runtime +namespace AlicizaX.UI { [Serializable] [ControlerStateName("GameObject/Visiblity")] diff --git a/Runtime/UXComponent/Controller/UXController.cs b/Runtime/UXComponent/Controller/UXController.cs index 3c453d3..657461a 100644 --- a/Runtime/UXComponent/Controller/UXController.cs +++ b/Runtime/UXComponent/Controller/UXController.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; -namespace AlicizaX.UI.Runtime +namespace AlicizaX.UI { [DisallowMultipleComponent] [AddComponentMenu("Controller/控制器")] diff --git a/Runtime/UXComponent/Controller/UXControllerStateRecorder.cs b/Runtime/UXComponent/Controller/UXControllerStateRecorder.cs index 00b5ff3..60f098f 100644 --- a/Runtime/UXComponent/Controller/UXControllerStateRecorder.cs +++ b/Runtime/UXComponent/Controller/UXControllerStateRecorder.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; using UnityEngine; -using UnityEngine.Serialization; -using Random = UnityEngine.Random; - -namespace AlicizaX.UI.Runtime +namespace AlicizaX.UI { [DisallowMultipleComponent] [AddComponentMenu("Controller/控制器状态")] diff --git a/Runtime/UXComponent/Group/UXGroup.cs b/Runtime/UXComponent/Group/UXGroup.cs index 8c25022..8ef1ca0 100644 --- a/Runtime/UXComponent/Group/UXGroup.cs +++ b/Runtime/UXComponent/Group/UXGroup.cs @@ -4,136 +4,140 @@ using UnityEngine.Events; using UnityEngine.EventSystems; -[DisallowMultipleComponent] -public class UXGroup : UIBehaviour +namespace UnityEngine.UI { - [SerializeField] private bool m_AllowSwitchOff; - [SerializeField] private List m_Buttons = new(); - - private UXButton _current; - - public UnityEvent onSelectedChanged = new(); - - public bool allowSwitchOff + [DisallowMultipleComponent] + [ExecuteAlways] + public class UXGroup : UIBehaviour { - get => m_AllowSwitchOff; - set + [SerializeField] private bool m_AllowSwitchOff; + [SerializeField] private List m_Buttons = new(); + + private UXButton _current; + + public UnityEvent onSelectedChanged = new(); + + public bool allowSwitchOff { - m_AllowSwitchOff = value; + get => m_AllowSwitchOff; + set + { + m_AllowSwitchOff = value; + ValidateGroup(); + } + } + + protected override void Start() => ValidateGroup(); + + protected override void OnDestroy() + { + foreach (var btn in m_Buttons) + if (btn) + btn.InternalTogSelected = false; + m_Buttons.Clear(); + base.OnDestroy(); + } + + public void RegisterButton(UXButton button) + { + if (!button) return; + if (m_Buttons.Contains(button)) return; + m_Buttons.Add(button); + + if (button.InternalTogSelected) + { + if (_current && _current != button) + _current.InternalTogSelected = false; + _current = button; + } + ValidateGroup(); } - } - protected override void Start() => ValidateGroup(); - - protected override void OnDestroy() - { - foreach (var btn in m_Buttons) - if (btn) - btn.InternalTogSelected = false; - m_Buttons.Clear(); - base.OnDestroy(); - } - - public void RegisterButton(UXButton button) - { - if (!button) return; - if (m_Buttons.Contains(button)) return; - m_Buttons.Add(button); - - if (button.InternalTogSelected) + public void UnregisterButton(UXButton button) { - if (_current && _current != button) - _current.InternalTogSelected = false; - _current = button; + if (!button) return; + m_Buttons.Remove(button); + if (_current == button) + _current = null; + button.InternalTogSelected = false; } - ValidateGroup(); - } + internal void NotifyButtonClicked(UXButton button) + { + if (!button) return; - public void UnregisterButton(UXButton button) - { - if (!button) return; - m_Buttons.Remove(button); - if (_current == button) + if (button.InternalTogSelected) + { + if (m_AllowSwitchOff) SetSelected(null); + } + else + { + SetSelected(button); + } + } + + private void SetSelected(UXButton target) + { + if (_current == target) return; + var previous = _current; _current = null; - button.InternalTogSelected = false; - } - internal void NotifyButtonClicked(UXButton button) - { - if (!button) return; + foreach (var btn in m_Buttons) + { + bool select = (btn == target); + if (btn.InternalTogSelected != select) + btn.InternalTogSelected = select; + if (select) _current = btn; + } - if (button.InternalTogSelected) - { - if (m_AllowSwitchOff) SetSelected(null); - } - else - { - SetSelected(button); - } - } - - private void SetSelected(UXButton target) - { - if (_current == target) return; - var previous = _current; - _current = null; - - foreach (var btn in m_Buttons) - { - bool select = (btn == target); - if (btn.InternalTogSelected != select) - btn.InternalTogSelected = select; - if (select) _current = btn; + if (_current) _current.Focus(); + if (previous != _current) + onSelectedChanged?.Invoke(_current); } - if (_current) _current.Focus(); - if (previous != _current) - onSelectedChanged?.Invoke(_current); - } - - private void ValidateGroup() - { - if (_current != null && _current.InternalTogSelected) return; - - if (!m_AllowSwitchOff && m_Buttons.Count > 0) - SetSelected(m_Buttons[0]); - } - - public bool AnyOtherSelected(UXButton exclude) - { - foreach (var btn in m_Buttons) - if (btn != exclude && btn.InternalTogSelected) - return true; - return false; - } - - public void SelectNext() => SelectRelative(1); - public void SelectPrevious() => SelectRelative(-1); - - private void SelectRelative(int dir) - { - if (m_Buttons.Count == 0) return; - int start = _current ? m_Buttons.IndexOf(_current) : -1; - int next = FindNextSelectable(start, dir); - if (next >= 0) SetSelected(m_Buttons[next]); - } - - private int FindNextSelectable(int startIndex, int dir) - { - if (m_Buttons.Count == 0) return -1; - int count = m_Buttons.Count; - int index = (startIndex + dir + count) % count; - - for (int i = 0; i < count; i++) + private void ValidateGroup() { - var btn = m_Buttons[index]; - if (btn && btn.isActiveAndEnabled && btn.Interactable) - return index; - index = (index + dir + count) % count; + if (_current != null && _current.InternalTogSelected) return; + + if (!m_AllowSwitchOff && m_Buttons.Count > 0) + SetSelected(m_Buttons[0]); } - return -1; + public bool AnyOtherSelected(UXButton exclude) + { + foreach (var btn in m_Buttons) + if (btn != exclude && btn.InternalTogSelected) + return true; + return false; + } + + public void SelectNext() => SelectRelative(1); + public void SelectPrevious() => SelectRelative(-1); + + private void SelectRelative(int dir) + { + if (m_Buttons.Count == 0) return; + int start = _current ? m_Buttons.IndexOf(_current) : -1; + int next = FindNextSelectable(start, dir); + if (next >= 0) SetSelected(m_Buttons[next]); + } + + private int FindNextSelectable(int startIndex, int dir) + { + if (m_Buttons.Count == 0) return -1; + int count = m_Buttons.Count; + int index = (startIndex + dir + count) % count; + + for (int i = 0; i < count; i++) + { + var btn = m_Buttons[index]; + if (btn && btn.isActiveAndEnabled && btn.Interactable) + return index; + index = (index + dir + count) % count; + } + + return -1; + } } } diff --git a/Runtime/UXComponent/Hotkey/HotkeyBindComponent.cs b/Runtime/UXComponent/Hotkey/HotkeyBindComponent.cs index f571a51..2041d30 100644 --- a/Runtime/UXComponent/Hotkey/HotkeyBindComponent.cs +++ b/Runtime/UXComponent/Hotkey/HotkeyBindComponent.cs @@ -2,7 +2,7 @@ using AlicizaX.UI.Runtime; using UnityEngine; -namespace AlicizaX.UI.Extension.UXComponent.Hotkey +namespace AlicizaX.UI { [DisallowMultipleComponent] public class HotkeyBindComponent : MonoBehaviour diff --git a/Runtime/UXComponent/Hotkey/UXHotkeyButton.cs b/Runtime/UXComponent/Hotkey/UXHotkeyButton.cs index 22ec8d6..c905e4f 100644 --- a/Runtime/UXComponent/Hotkey/UXHotkeyButton.cs +++ b/Runtime/UXComponent/Hotkey/UXHotkeyButton.cs @@ -1,11 +1,13 @@ +#if INPUTSYSTEM_SUPPORT using UnityEngine; +using UnityEngine.InputSystem; -namespace AlicizaX.UI.Extension +namespace AlicizaX.UI { [DisallowMultipleComponent] public class UXHotkeyButton : UXButton { - [SerializeField] internal UnityEngine.InputSystem.InputActionReference _hotKeyRefrence; + [SerializeField] internal InputActionReference _hotKeyRefrence; [SerializeField] internal EHotkeyPressType _hotkeyPressType; internal void HotkeyActionTrigger() @@ -17,3 +19,5 @@ namespace AlicizaX.UI.Extension } } } + +#endif diff --git a/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs b/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs index 729d33c..1726083 100644 --- a/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs +++ b/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs @@ -1,150 +1,153 @@ #if INPUTSYSTEM_SUPPORT +using System; using UnityEngine; using UnityEngine.InputSystem; using System.Collections.Generic; using System.Collections; -using AlicizaX.UI.Extension; +using AlicizaX.UI; +using UnityEditor.Callbacks; -internal enum EHotkeyPressType +namespace AlicizaX.UI { - Started, - Performed -} - -internal static class UXHotkeyRegisterManager -{ - private readonly struct HotkeyRegistration + internal enum EHotkeyPressType { - public readonly EHotkeyPressType pressType; - public readonly UXHotkeyButton button; - - public HotkeyRegistration(UXHotkeyButton btn, EHotkeyPressType pressType) - { - button = btn; - this.pressType = pressType; - } + Started, + Performed } - private static readonly Dictionary> _hotkeyRegistry = - new Dictionary>(32); + internal static class UXHotkeyRegisterManager + { + private readonly struct HotkeyRegistration + { + public readonly EHotkeyPressType pressType; + public readonly UXHotkeyButton button; + + public HotkeyRegistration(UXHotkeyButton btn, EHotkeyPressType pressType) + { + button = btn; + this.pressType = pressType; + } + } + + private static readonly Dictionary> _hotkeyRegistry = new(32); - private static readonly Dictionary handler, InputActionReference action)> _sharedHandlers = - new Dictionary, InputActionReference)>(32); + private static readonly Dictionary handler, InputActionReference action)> _sharedHandlers = + new(32); - private static readonly Dictionary _buttonRegistrations = - new Dictionary(64); + private static readonly Dictionary _buttonRegistrations = new(64); #if UNITY_EDITOR - [UnityEditor.Callbacks.DidReloadScripts] - internal static void ClearHotkeyRegistry() - { - foreach (var key in _buttonRegistrations.Keys) + [DidReloadScripts] + internal static void ClearHotkeyRegistry() { - UnregisterHotkey(key); - } + foreach (var key in _buttonRegistrations.Keys) + { + UnregisterHotkey(key); + } - _sharedHandlers.Clear(); - _hotkeyRegistry.Clear(); - _buttonRegistrations.Clear(); - } + _sharedHandlers.Clear(); + _hotkeyRegistry.Clear(); + _buttonRegistrations.Clear(); + } #endif - internal static void RegisterHotkey(UXHotkeyButton button, InputActionReference action, EHotkeyPressType pressType) - { - if (action == null || action.action == null || button == null) - return; - - string actionId = action.action.id.ToString(); - - - HotkeyRegistration registration = new HotkeyRegistration(button, pressType); - - - if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations)) + internal static void RegisterHotkey(UXHotkeyButton button, InputActionReference action, EHotkeyPressType pressType) { - registrations = new List(4); - _hotkeyRegistry[actionId] = registrations; - } + if (action == null || action.action == null || button == null) + return; - registrations.Add(registration); + string actionId = action.action.id.ToString(); - _buttonRegistrations[button] = actionId; + HotkeyRegistration registration = new HotkeyRegistration(button, pressType); - if (!_sharedHandlers.ContainsKey(actionId)) - { - System.Action handler = ctx => OnHotkeyTriggered(actionId); - _sharedHandlers[actionId] = (handler, action); - switch (pressType) + if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations)) { - case EHotkeyPressType.Started: - action.action.started += handler; - break; - case EHotkeyPressType.Performed: - action.action.performed += handler; - break; + registrations = new List(4); + _hotkeyRegistry[actionId] = registrations; } - action.action.Enable(); - } - } - - public static void UnregisterHotkey(UXHotkeyButton button) - { - if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId)) - return; + registrations.Add(registration); - if (_hotkeyRegistry.TryGetValue(actionId, out var registrations)) - { - HotkeyRegistration hotkeyInfo; - for (int i = registrations.Count - 1; i >= 0; i--) + _buttonRegistrations[button] = actionId; + + if (!_sharedHandlers.ContainsKey(actionId)) { - if (registrations[i].button == button) + Action handler = ctx => OnHotkeyTriggered(actionId); + _sharedHandlers[actionId] = (handler, action); + + switch (pressType) { - hotkeyInfo = registrations[i]; - registrations.RemoveAt(i); + case EHotkeyPressType.Started: + action.action.started += handler; + break; + case EHotkeyPressType.Performed: + action.action.performed += handler; + break; + } - if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo)) + action.action.Enable(); + } + } + + public static void UnregisterHotkey(UXHotkeyButton button) + { + if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId)) + return; + + + if (_hotkeyRegistry.TryGetValue(actionId, out var registrations)) + { + HotkeyRegistration hotkeyInfo; + for (int i = registrations.Count - 1; i >= 0; i--) + { + if (registrations[i].button == button) { - var (handler, actionRef) = handlerInfo; - if (actionRef != null && actionRef.action != null) - { - actionRef.action.Disable(); - _sharedHandlers.Remove(actionId); - _hotkeyRegistry.Remove(actionId); + hotkeyInfo = registrations[i]; + registrations.RemoveAt(i); - switch (hotkeyInfo.pressType) + if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo)) + { + var (handler, actionRef) = handlerInfo; + if (actionRef != null && actionRef.action != null) { - case EHotkeyPressType.Started: - actionRef.action.started -= handler; - break; - case EHotkeyPressType.Performed: - actionRef.action.performed -= handler; - break; + actionRef.action.Disable(); + _sharedHandlers.Remove(actionId); + _hotkeyRegistry.Remove(actionId); + + switch (hotkeyInfo.pressType) + { + case EHotkeyPressType.Started: + actionRef.action.started -= handler; + break; + case EHotkeyPressType.Performed: + actionRef.action.performed -= handler; + break; + } } } - } - break; + break; + } } } + + + _buttonRegistrations.Remove(button); } - - _buttonRegistrations.Remove(button); - } - - private static void OnHotkeyTriggered(string actionId) - { - if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0) + private static void OnHotkeyTriggered(string actionId) { - var registration = registrations[^1]; - registration.button.HotkeyActionTrigger(); + if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0) + { + var registration = registrations[^1]; + registration.button.HotkeyActionTrigger(); + } } } } diff --git a/Runtime/UXComponent/UX/UXDraggable.cs b/Runtime/UXComponent/UX/UXDraggable.cs index 31ca356..86bc306 100644 --- a/Runtime/UXComponent/UX/UXDraggable.cs +++ b/Runtime/UXComponent/UX/UXDraggable.cs @@ -2,7 +2,7 @@ using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; -namespace AlicizaX.UI.Extension +namespace AlicizaX.UI { public class UXDraggable:MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {