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");
        };
    }
}
相关推荐
叶帆9 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君9 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子10 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai31524754310 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光10 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光10 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯10 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案10 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔10 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
鼎艺创新科技10 天前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d