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");
        };
    }
}
相关推荐
mxwin1 小时前
Unity Shader Alpha测试 · 模板测试 · 深度测试
unity·游戏引擎
2601_956002814 小时前
冬日狂想曲(赠去马赛克补丁)2026.5.13最新版免费下载 转存后自动更新 (看到请立即转存 资源随时失效)pc手机版通用
智能手机·游戏引擎·电脑·游戏程序·动画·游戏美术
Sator14 小时前
unity解决粒子与物体接触时的硬边缘问题
unity·游戏引擎
RPGMZ7 小时前
RPGMZ NPC头顶自动显示一段消息
前端·游戏引擎·rpgmz·rpgmakermz
程序员JerrySUN8 小时前
Jetson边缘嵌入式实战课程第三讲:L4T 与 Jetson 系统架构
linux·服务器·人工智能·安全·unity·系统架构·游戏引擎
萌萌的提莫队长10 小时前
Unity HDRP 渲染管线 Camera 输出到RenderTexture没有Alpha通道
unity·游戏引擎
winlife_10 小时前
Unity Editor 工具不该用反射写组件字段:SerializedObject 在 4 个场景里非用不可
unity·自动化·游戏引擎
星河耀银海10 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
郝学胜-神的一滴11 小时前
中级OpenGL教程 005:为球体&平面注入法线灵魂
c++·unity·图形渲染·three.js·opengl·unreal
那个村的李富贵11 小时前
unity编辑器工具,输出使用的字体
unity·编辑器·游戏引擎