From 5e256faf03438883b8861bb41dadbdca8738493f 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, 17 Mar 2026 20:08:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=9A=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=BB=91=E5=AE=9A=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomeModule/InputGlyph/GlyphService.cs | 6 ++ .../InputGlyph/InputGlyphBehaviourBase.cs | 8 +-- .../InputGlyph/InputGlyphDatabase.cs | 62 ++++++++++++++++++- .../InputGlyph/InputGlyphText.cs | 3 +- .../InputGlyph/InputGlyphUXButton.cs | 3 +- .../InputGlyph/TestRebindScript.cs | 6 +- 6 files changed, 76 insertions(+), 12 deletions(-) diff --git a/Client/Assets/Scripts/CustomeModule/InputGlyph/GlyphService.cs b/Client/Assets/Scripts/CustomeModule/InputGlyph/GlyphService.cs index d542fbb..4b7df7c 100644 --- a/Client/Assets/Scripts/CustomeModule/InputGlyph/GlyphService.cs +++ b/Client/Assets/Scripts/CustomeModule/InputGlyph/GlyphService.cs @@ -138,6 +138,12 @@ public static class GlyphService return string.Empty; } + string humanReadable = InputControlPath.ToHumanReadableString(controlPath, InputControlPath.HumanReadableStringOptions.OmitDevice); + if (!string.IsNullOrWhiteSpace(humanReadable)) + { + return humanReadable; + } + string[] parts = controlPath.Split('/'); string last = parts[parts.Length - 1].Trim(TrimChars); return last; diff --git a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphBehaviourBase.cs b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphBehaviourBase.cs index e5d9811..949f1ca 100644 --- a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphBehaviourBase.cs +++ b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphBehaviourBase.cs @@ -7,20 +7,20 @@ public abstract class InputGlyphBehaviourBase : MonoBehaviour protected virtual void OnEnable() { CurrentCategory = InputDeviceWatcher.CurrentCategory; - InputDeviceWatcher.OnDeviceContextChanged += HandleDeviceContextChanged; + InputDeviceWatcher.OnDeviceChanged += HandleDeviceChanged; InputBindingManager.BindingsChanged += HandleBindingsChanged; RefreshGlyph(); } protected virtual void OnDisable() { - InputDeviceWatcher.OnDeviceContextChanged -= HandleDeviceContextChanged; + InputDeviceWatcher.OnDeviceChanged -= HandleDeviceChanged; InputBindingManager.BindingsChanged -= HandleBindingsChanged; } - private void HandleDeviceContextChanged(InputDeviceWatcher.DeviceContext context) + private void HandleDeviceChanged(InputDeviceWatcher.InputDeviceCategory category) { - CurrentCategory = context.Category; + CurrentCategory = category; RefreshGlyph(); } diff --git a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphDatabase.cs b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphDatabase.cs index 7a5911c..7d4084e 100644 --- a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphDatabase.cs +++ b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphDatabase.cs @@ -231,9 +231,65 @@ public sealed class InputGlyphDatabase : ScriptableObject private static string NormalizeControlPath(string controlPath) { - return string.IsNullOrWhiteSpace(controlPath) - ? string.Empty - : controlPath.Trim().ToLowerInvariant(); + if (string.IsNullOrWhiteSpace(controlPath)) + { + return string.Empty; + } + + return CanonicalizeDeviceLayout(controlPath.Trim().ToLowerInvariant()); + } + + private static string CanonicalizeDeviceLayout(string controlPath) + { + int start = controlPath.IndexOf('<'); + int end = controlPath.IndexOf('>'); + if (start < 0 || end <= start + 1) + { + return controlPath; + } + + string layout = controlPath.Substring(start + 1, end - start - 1); + string canonicalLayout = GetCanonicalLayout(layout); + if (string.Equals(layout, canonicalLayout, StringComparison.Ordinal)) + { + return controlPath; + } + + return controlPath.Substring(0, start + 1) + canonicalLayout + controlPath.Substring(end); + } + + private static string GetCanonicalLayout(string layout) + { + if (string.IsNullOrEmpty(layout)) + { + return string.Empty; + } + + if (layout.IndexOf("keyboard", StringComparison.OrdinalIgnoreCase) >= 0) + { + return "keyboard"; + } + + if (layout.IndexOf("mouse", StringComparison.OrdinalIgnoreCase) >= 0) + { + return "mouse"; + } + + if (layout.IndexOf("joystick", StringComparison.OrdinalIgnoreCase) >= 0) + { + return "joystick"; + } + + if (layout.IndexOf("gamepad", StringComparison.OrdinalIgnoreCase) >= 0 + || layout.IndexOf("controller", StringComparison.OrdinalIgnoreCase) >= 0 + || layout.IndexOf("xinput", StringComparison.OrdinalIgnoreCase) >= 0 + || layout.IndexOf("dualshock", StringComparison.OrdinalIgnoreCase) >= 0 + || layout.IndexOf("dualsense", StringComparison.OrdinalIgnoreCase) >= 0) + { + return "gamepad"; + } + + return layout; } private static InputDeviceWatcher.InputDeviceCategory ParseCategory(string deviceName) diff --git a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphText.cs b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphText.cs index dbf83c8..a2f7754 100644 --- a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphText.cs +++ b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphText.cs @@ -7,6 +7,7 @@ using UnityEngine.InputSystem; public sealed class InputGlyphText : InputGlyphBehaviourBase { [SerializeField] private InputActionReference actionReference; + [SerializeField] private string compositePartName; private TMP_Text _textField; private string _templateText; @@ -35,7 +36,7 @@ public sealed class InputGlyphText : InputGlyphBehaviourBase } string formattedText; - if (GlyphService.TryGetTMPTagForActionPath(actionReference, string.Empty, CurrentCategory, out string tag, out string displayFallback)) + if (GlyphService.TryGetTMPTagForActionPath(actionReference, compositePartName, CurrentCategory, out string tag, out string displayFallback)) { formattedText = Utility.Text.Format(_templateText, tag); } diff --git a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphUXButton.cs b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphUXButton.cs index ae95bac..8d36ecf 100644 --- a/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphUXButton.cs +++ b/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphUXButton.cs @@ -6,6 +6,7 @@ using UnityEngine.UI; public sealed class InputGlyphUXButton : InputGlyphBehaviourBase { [SerializeField] private UXButton button; + [SerializeField] private string compositePartName; [SerializeField] private Image targetImage; private Sprite _cachedSprite; @@ -49,7 +50,7 @@ public sealed class InputGlyphUXButton : InputGlyphBehaviourBase return; } - bool hasSprite = GlyphService.TryGetUISpriteForActionPath(actionReference, string.Empty, CurrentCategory, out Sprite sprite); + bool hasSprite = GlyphService.TryGetUISpriteForActionPath(actionReference, compositePartName, CurrentCategory, out Sprite sprite); if (!hasSprite) { sprite = null; diff --git a/Client/Assets/Scripts/CustomeModule/InputGlyph/TestRebindScript.cs b/Client/Assets/Scripts/CustomeModule/InputGlyph/TestRebindScript.cs index dd541af..f8cc3bb 100644 --- a/Client/Assets/Scripts/CustomeModule/InputGlyph/TestRebindScript.cs +++ b/Client/Assets/Scripts/CustomeModule/InputGlyph/TestRebindScript.cs @@ -27,7 +27,7 @@ public class TestRebindScript : MonoBehaviour private void Start() { if (btn != null) btn.onClick.AddListener(OnBtnClicked); - InputDeviceWatcher.OnDeviceContextChanged += OnDeviceContextChanged; + InputDeviceWatcher.OnDeviceChanged += OnDeviceChanged; InputBindingManager.BindingsChanged += OnBindingsChanged; UpdateBindingText(); @@ -47,7 +47,7 @@ public class TestRebindScript : MonoBehaviour private void OnDisable() { if (btn != null) btn.onClick.RemoveListener(OnBtnClicked); - InputDeviceWatcher.OnDeviceContextChanged -= OnDeviceContextChanged; + InputDeviceWatcher.OnDeviceChanged -= OnDeviceChanged; InputBindingManager.BindingsChanged -= OnBindingsChanged; if (InputBindingManager.Instance != null) @@ -117,7 +117,7 @@ public class TestRebindScript : MonoBehaviour /// /// 设备变更的回调 /// - private void OnDeviceContextChanged(InputDeviceWatcher.DeviceContext _) + private void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory _) { UpdateBindingText(); }