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");
        };
    }
}
相关推荐
Duo1J3 小时前
【UE】Slate 编辑器工具开发03 - 节点编辑器 (EdGraph)
ue5·编辑器·游戏引擎·ue4
郝学胜-神的一滴4 小时前
[简化版 GAMES 104] 现代游戏引擎 02:拆解现代游戏引擎5+1层级架构,吃透引擎底层核心逻辑
c++·unity·架构·游戏引擎·图形渲染·unreal engine·系统设计
LONGZETECH5 小时前
新能源汽车充电设备装配与调试仿真教学软件 技术架构与核心实现解析
大数据·算法·3d·unity·架构·汽车
丁小未6 小时前
Unity 几种常见合批手段的要求
游戏·unity·合批·srpbatcher·动态合批·静态合批
旧物有情1 天前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
勇踏前人未索之境1 天前
Unity打包运行于鸿蒙手机
unity·智能手机·harmonyos
玖玥拾1 天前
Unity 3D 笔记(十一)UI 框架进阶:栈弹窗交互、BasePanel 基类虚方法、DoTween 界面动画
笔记·3d·unity
郝学胜-神的一滴1 天前
中级OpenGL教程 023:Assimp模型加载全解——从源码到架构的骈文探秘
c++·unity·游戏引擎·cmake·unreal engine·opengl
xcLeigh2 天前
Unity基础:GameObject与Component——Unity核心架构思想彻底理解
unity·教程·component·gameobject
郝学胜-神的一滴2 天前
中级OpenGL教程 022:探秘三维世界的血脉传承——物体父子关系与矩阵递归奥义
c++·线性代数·unity·矩阵·游戏引擎·unreal engine·opengl