优化多平台设备绑定组件

This commit is contained in:
陈思海 2026-03-17 20:08:19 +08:00
parent f4ec668bfd
commit 5e256faf03
6 changed files with 76 additions and 12 deletions

View File

@ -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;

View File

@ -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();
}

View File

@ -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)

View File

@ -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);
}

View File

@ -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;

View File

@ -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
/// <summary>
/// 设备变更的回调
/// </summary>
private void OnDeviceContextChanged(InputDeviceWatcher.DeviceContext _)
private void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory _)
{
UpdateBindingText();
}