AlicizaX/Client/Assets/InputGlyph/InputGlyphText.cs
陈思海 5862fb2af1 add
1.新增手柄相关适配
2.新增设备重映射绑定
3.新增设备映射图标更新
2025-12-09 20:31:44 +08:00

55 lines
1.6 KiB
C#

using System;
using System.Linq;
using AlicizaX;
using AlicizaX.InputGlyph;
using UnityEngine;
using TMPro;
using UnityEngine.InputSystem;
[RequireComponent(typeof(TextMeshProUGUI))]
public class InputGlyphText : MonoBehaviour
{
[SerializeField] private InputActionReference actionReference;
private TMP_Text textField;
void OnEnable()
{
if (textField == null) textField = GetComponent<TMP_Text>();
InputDeviceWatcher.OnDeviceChanged += OnDeviceChanged;
UpdatePrompt();
}
void OnDisable()
{
InputDeviceWatcher.OnDeviceChanged -= OnDeviceChanged;
}
void OnBindingChanged(InputAction action, int idx)
{
if (actionReference == null || actionReference.action == null) return;
if (action == actionReference.action) UpdatePrompt();
}
void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory cat)
{
UpdatePrompt();
}
void UpdatePrompt()
{
if (actionReference == null || actionReference.action == null || textField == null) return;
var action = actionReference.action;
string path = GlyphService.GetBindingControlPath(action);
var device = InputDeviceWatcher.CurrentCategory;
string displayFallback = string.Empty;
string oldText = textField.text;
if (GlyphService.TryGetTMPTagForActionPath(path, device, out string tag, out displayFallback))
{
textField.text = Utility.Text.Format(oldText, tag);
return;
}
textField.text = Utility.Text.Format(oldText, displayFallback);;
}
}