【问题标题】:Interactive rebinding of composite binds复合绑定的交互式重新绑定
【发布时间】:2022-06-22 19:41:42
【问题描述】:

我正在制作控制设置菜单以让玩家更改设置。我的控件使用新的统一输入系统制作。我已经编写了脚本来重新绑定简单的绑定,例如跳跃或使用技能。但我的动作绑定为复合 Vector2 绑定。这是我在每个重新绑定按钮上放置的重新绑定脚本:

using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
using UnityEngine.EventSystems;
 
public class RebindButton : MonoBehaviour
{
    [SerializeField] private InputActionReference inputActionRef;
    [SerializeField] private TMP_Text buttonText;
    private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
    private void Start()
    {
        int bindingIndex = inputActionRef.action.GetBindingIndexForControl(inputActionRef.action.controls[0]);
        buttonText.text = InputControlPath.ToHumanReadableString(inputActionRef.action.bindings[bindingIndex].effectivePath,
            InputControlPath.HumanReadableStringOptions.OmitDevice);
    }
    public void StartRebinding()
    {
        rebindingOperation = inputActionRef.action.PerformInteractiveRebinding()
                    .WithCancelingThrough("<Keyboard>/escape")
                    .WithControlsExcluding("Mouse")
                    .OnMatchWaitForAnother(0.1f)
                    .OnCancel(operation => FindObjectOfType<EventSystem>().SetSelectedGameObject(null))
                    .OnComplete(operation => RebindComplete())
                    .Start();
    }
 
    private void RebindComplete()
    {
        int bindingIndex = inputActionRef.action.GetBindingIndexForControl(inputActionRef.action.controls[0]);
        buttonText.text = InputControlPath.ToHumanReadableString(inputActionRef.action.bindings[bindingIndex].effectivePath,
            InputControlPath.HumanReadableStringOptions.OmitDevice);
        rebindingOperation.Dispose();
        FindObjectOfType<EventSystem>().SetSelectedGameObject(null);
    }
}

输入系统如下所示:

我应该使用什么来重新绑定我的动作。现在我不能这样做,因为我不能选择 MoveUp 动作作为输入动作参考。我唯一可以选择的是整个运动。我知道我的问题的关键是绑定索引变量,但我不知道如何使此代码同时用于复合绑定和简单绑定。

我尝试使用 .withTargetBinding,但并不真正了解如何为其获取必要的 ID。

【问题讨论】:

    标签: c# unity3d input inputbinding


    【解决方案1】:

    Unity 有很好的示例来说明 Vector2 绑定的这种情况。

    打开包管理器,选择输入系统包,然后选择 示例项目“重新绑定 UI”下载。

    在您的运动合成中,索引 0 是合成绑定本身,1 是“上:W”,2 是“下:S”等。

    要使用复合绑定和常规绑定,您可以在绑定中检查 .isComposite 标志。然后对非复合使用 0 索引,对复合使用 >0。

    There is short example of rebind UI

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2010-09-14
      • 2014-11-08
      • 1970-01-01
      相关资源
      最近更新 更多