Unity InputSystem自定义InputDevice及一个Button的示例:

1、声明一个 MyInputDevice

cs 复制代码
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Utilities;

[InputControlLayout(stateType = typeof(MyInputDeviceState))] //关联上 MyInputDeviceState
public class MyInputDevice : InputDevice
{

}
public struct MyInputDeviceState : IInputStateTypeInfo //写一个继承自"IInputStateTypeInfo接口"的结构体
{
    public FourCC format => new FourCC('T', 'E', 'S', 'T'); //这里的四个字符可以自定义,表示设备识别符(ID)
    [InputControl(name = "button", layout = "Button")] //这里的layout = 是必填项,表示是个按钮
    public bool button;
}

2、通过按键盘的空格键,模拟MyInputDevice的按钮的输入

cs 复制代码
using UnityEngine;
using UnityEngine.InputSystem;

/// <summary>
/// 通过键盘的空格键,模拟MyInputDevice的按钮的输入
/// </summary>
public class MyInputDevice_MoNi : MonoBehaviour
{
    private MyInputDevice myInputDevice;

    private void Start()
    {
        //注册 + 增加,这两步是必须的
        InputSystem.RegisterLayout<MyInputDevice>();
        myInputDevice = InputSystem.AddDevice<MyInputDevice>();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //按下空格键,模拟设备按钮的按下
            InputSystem.QueueStateEvent(myInputDevice, new MyInputDeviceState() { button = true });
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            //松开空格键,模拟设备按钮的松开
            InputSystem.QueueStateEvent(myInputDevice, new MyInputDeviceState() { button = false });
        }
    }
}

3、构造相关的 InputAction,测试:

① 新建一个UnityActions的Asset文件,(Action Type是Button),Binding的Path填入:<MyInputDevice>/button ,按回车键确认

② 写测试代码:

cs 复制代码
using UnityEngine;
using UnityEngine.InputSystem;

public class InputAction_Funner : MonoBehaviour
{
    public InputActionReference inputActionReference;
    private InputAction inputAction;

    void Start()
    {
        inputAction = inputActionReference.action;
        inputAction.Enable();

        inputAction.started += (v) =>
        {
            Debug.LogError("start");
        };
        inputAction.performed += (v) =>
        {
            Debug.LogError("performed");
        };
        inputAction.canceled += (v) =>
        {
            Debug.LogError("cancel");
        };
    }
}

运行,按空格键,即可看到效果:

在第一次运行后,下次绑定Path,可直接找到Path的 Other 选项中的 MyInputDevice:




方式二:

cs 复制代码
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;


public class MyInputDevice : InputDevice
{
    [InputControl]
    public ButtonControl button { get; private set; }

    protected override void FinishSetup()
    {
        base.FinishSetup();
        button = GetChildControl<ButtonControl>(nameof(button));
    }
}
cs 复制代码
using UnityEngine;
using UnityEngine.InputSystem;

/// <summary>
/// 通过键盘的空格键,模拟MyInputDevice的按钮的输入
/// </summary>
public class MyInputDevice_MoNi : MonoBehaviour
{
    private MyInputDevice myInputDevice;

    private void Start()
    {
        //注册 + 增加,这两步是必须的
        InputSystem.RegisterLayout<MyInputDevice>();
        myInputDevice = InputSystem.AddDevice<MyInputDevice>();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //按下空格键,模拟设备按钮的按下
            //InputSystem.QueueStateEvent(myInputDevice, new MyInputDeviceState() { button = true });
            InputSystem.QueueDeltaStateEvent(myInputDevice.button, true);
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            //松开空格键,模拟设备按钮的松开
            //InputSystem.QueueStateEvent(myInputDevice, new MyInputDeviceState() { button = false });
            InputSystem.QueueDeltaStateEvent(myInputDevice.button, false);
        }
    }
}
cs 复制代码
using UnityEngine;
using UnityEngine.InputSystem;

public class InputAction_Funner : MonoBehaviour
{
    public InputActionReference inputActionReference;
    private InputAction inputAction;

    void Start()
    {
        inputAction = inputActionReference.action;
        inputAction.Enable();

        inputAction.started += (v) =>
        {
            Debug.LogError("start");
        };
        inputAction.performed += (v) =>
        {
            Debug.LogError("performed");
        };
        inputAction.canceled += (v) =>
        {
            Debug.LogError("cancel");
        };
    }
}
相关推荐
风酥糖20 小时前
Godot游戏练习01-第20节-增加亿点点细节
游戏·游戏引擎·godot
智算菩萨21 小时前
【OpenGL】6 真实感光照渲染实战:Phong模型、材质系统与PBR基础
开发语言·python·游戏引擎·游戏程序·pygame·材质·opengl
心前阳光1 天前
Unity之ScrollRect简易实现
unity·游戏引擎
WarrenMondeville1 天前
9.Unity面向对象-对象池
unity
KaGme1 天前
生成3DGS场景在unity中的呈现
3d·unity·游戏引擎
zyh______2 天前
关于unity的序列化
unity·游戏引擎
weixin_409383122 天前
godot碰撞测试的学习
学习·游戏引擎·godot
电子云与长程纠缠2 天前
Godot学习06 - AnimationPlayer内置动画
学习·游戏引擎·godot
星夜泊客2 天前
C# : 引用类型都存在堆上吗
unity·c#
点量云实时渲染-小芹2 天前
Unity模型数字孪生虚拟仿真webgl推流卡实时云渲染推流
unity·webgl·数字孪生·实时云渲染·虚拟仿真·云推流