Unity New Input System

Unity New Input System,第1张

目录

一、概述

二、设置

三、案例

四、使用步骤(InvokeUnityEvents)

1)右键/Create/InputActions,新建一个输入控制器;

 2)双击打开,你会看到下面一个d窗;

3)添加一个ActionMaps/Actions,再设置一下属性。

 4)添加一个PlayerInput组件

 5)写一个脚本,让Sphere跳一下

 6)测试一下

 7)状态条件

五、Invoke C Sharp Events

六、Interactions/Hold

 七、(处理器)Processors/Invert

 八、将InputActions转换成C#类

九、Action  ActionType:Value   ControlType:Vector2

十、多重管理

 十一、输入设备分析InputDebug

十二、ActionType

十三、直接使用现有输入

十四、开启关闭ActionMaps

十五、重新绑定事件


一、概述

前段时间使用unity2020版本,发现自己的鼠标输入事件不起作用了!仔细看了看,原来是启用的新版的输入系统,但是代码确实Old。

二、设置

首先你得先告诉引擎你要使用哪个输入系统,当选择Both的时候就是两个都兼容。

当支持的设备这一栏中为空时,说明支持所有设备。

https://docs.unity.cn/Packages/[email protected]/manual/Settings.html

三、案例

如果你想看看官方的案例,看下图。从PackagesManager里下载即可。

四、使用步骤(InvokeUnityEvents)

可以参考官方,毕竟官方的东西靠谱!https://docs.unity.cn/Packages/[email protected]/manual/QuickStartGuide.html

1)右键/Create/InputActions,新建一个输入控制器;

 2)双击打开,你会看到下面一个d窗;

3)添加一个ActionMaps/Actions,再设置一下属性。

 设置绑定的Path时,可以点击Listen,然后按相应的输入 *** 作即可。

这里的ActionMaps中的Player对应的就是Events中事件集合,Actions中的Jump就是一个具体的事件,Jump下级中的Space就是具体的输入 *** 作了,你可以添加多个,都是触发Jump这个事件。

 4)添加一个PlayerInput组件

Actions选择之前做好的,Behavior选择InvokeUnityEvents

 5)写一个脚本,让Sphere跳一下

将脚本挂载上去,在PlayerInput中的Events时间中绑定该方法。

using UnityEngine;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private void Awake()
    {
        rigidbody = GetComponent();
    }
    public void Jump()
    {
        Debug.Log("Jump!");
        rigidbody.AddForce(Vector3.up * 5f, ForceMode.Impulse);
    }
}

 6)测试一下

此时按下space时执行了两次,抬起时执行了一次。第一次是按下,第二次是记录按下按键的执行,第三次是抬起。

添加一下事件回调

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private void Awake()
    {
        rigidbody = GetComponent();
    }
    public void Jump(InputAction.CallbackContext context)
    {
        Debug.Log("Jump!"+context.phase);
        rigidbody.AddForce(Vector3.up * 5f, ForceMode.Impulse);
    }
}

 再测试一下,三个状态就呈现出来了:Started  Performed  Canceled

 7)状态条件

添加一个判断条件,只在Performed的时候执行

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private void Awake()
    {
        rigidbody = GetComponent();
    }
    public void Jump(InputAction.CallbackContext context)
    {
        if(context.performed)
        {
            Debug.Log("Jump!" + context.phase);
            rigidbody.AddForce(Vector3.up, ForceMode.Impulse);
        }
    }
}
五、Invoke C Sharp Events

来看看不用手动拖拽的绑定方式

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private PlayerInput playerInput;
    private void Awake()
    {
        rigidbody = GetComponent();
        playerInput = GetComponent();
        playerInput.onActionTriggered += PlayerInput_onActionTriggered;
    }
    private void PlayerInput_onActionTriggered(InputAction.CallbackContext context)
    {
        Debug.Log(context);
    }
}

测试 

六、Interactions/Hold

输入事件持续时间,当小于该时间时不触发context.performed

此处的Hold中PressPoint的默认设置对应InputSystemPackage中的DefaultButtonPressPoint,HoldTime类似。当然你也可以自定义。

Interactions中还有其他设置,有需要可以去官方看详细内容。

 七、(处理器)Processors/Invert

Invert是一个反转器,当添加之后,其value值就变成相反数了。

 八、将InputActions转换成C#类

如图,选中GenerateC#Class,Apply即可。

 接下来就可以使用这个类了,这样就不用挂载PlayerInput这个组件了。

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private void Awake()
    {
        rigidbody = GetComponent();

        PlayerInputActions inputActions = new PlayerInputActions();
        inputActions.Player.Enable();//确保实例化的PlayerInputActions起作用,不然Awake结束后,该实例就被销毁了。
        inputActions.Player.Jump.performed += Jump;
    }

    public void Jump(InputAction.CallbackContext context)
    {
        Debug.Log(context);
        if(context.performed)
        {
            Debug.Log("Jump!" + context.phase);
            rigidbody.AddForce(Vector3.up, ForceMode.Impulse);
        }
    }
}
九、Action  ActionType:Value   ControlType:Vector2

添加一个2DVector,分别绑定输入事件。设置完成后点击SaveAsset保存一下,据此生产C#脚本也会同步更新。

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private void Awake()
    {
        rigidbody = GetComponent();

        PlayerInputActions inputActions = new PlayerInputActions();
        inputActions.Player.Enable();
        inputActions.Player.Jump.performed += Jump;

        inputActions.Player.Movement.performed += MovementPerformed;
    }
    //每点击一次触发一次
    public void MovementPerformed(InputAction.CallbackContext context)
    {
        Debug.Log(context);
        Vector2 inputVector = context.ReadValue();
        rigidbody.AddForce(new Vector3(inputVector.x, 0, inputVector.y), ForceMode.Force);
    }
}

如果你想在按住输入时持续触发事件,那么可以在Update函数中实现

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private PlayerInputActions inputActions;
    private void Awake()
    {
        rigidbody = GetComponent();

        inputActions = new PlayerInputActions();
        inputActions.Player.Enable();
        inputActions.Player.Movement.performed += MovementPerformed;
    }
    //当一直按住w键时,inputVector.x一直为1,当抬起后为0
    private void Update()
    {
        Vector2 inputVector = inputActions.Player.Movement.ReadValue();
        rigidbody.AddForce(new Vector3(inputVector.x, 0, inputVector.y), ForceMode.Force);
    }
}
十、多重管理

Add Control Schemes,然后将输入事件绑定进去。切换不同的ActionMaps,你会发现Actions是不变的,但是Actions下级的输入事件不同了;这样你就可以对同一事件绑定不同外部设备的输入,这样跨平台的 *** 作就极为方便了。不同外部设备的输入 *** 作就可以触发同一的事件了!

 再建一个Control Schemes,你会发现选择Gamepad时,没有绑定的输入事件了。

 十一、输入设备分析InputDebug

如果你发现你想输入的事件无法找到,那么你可以先看看自己的电脑有没有连接相应的设备。

十二、ActionType

https://docs.unity.cn/Packages/[email protected]/api/UnityEngine.InputSystem.InputActionType.html?q=pass%20throu

十三、直接使用现有输入

官方建议仅测试的时候使用

 private void Update()
    {
        if(Mouse.current.leftButton.wasPressedThisFrame)
        {
            Debug.Log("mouse clicked!");
        }
        if(Keyboard.current.tKey.wasPressedThisFrame)
        {
            Debug.Log("tKey clicked!");
        }
    }
十四、开启关闭ActionMaps

如果使用的PlayerInput组件的话,用下面的方法;如果是已经转换成了C#脚本,那么直接设置其是否可获取即可,即:

PlayerInputActions inputActions = new PlayerInputActions();
                               inputActions.Player.Enable();

using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private PlayerInput playerInput;
    private void Awake()
    {
        rigidbody = GetComponent();
        playerInput = GetComponent();
    }
    private void Update()
    {
        if(Keyboard.current.tKey.wasPressedThisFrame)
        {
            //开启名称为UI的ActionMap
            playerInput.SwitchCurrentActionMap("UI");
        }
    }
    public void Jump(InputAction.CallbackContext context)
    {
        Debug.Log(context);
        if(context.performed)
        {
            Debug.Log("Jump!" + context.phase);
            rigidbody.AddForce(Vector3.up, ForceMode.Impulse);
        }
    }
}
十五、重新绑定事件
using UnityEngine;
using UnityEngine.InputSystem;

public class TestingInputSystem : MonoBehaviour
{
    private Rigidbody rigidbody;
    private PlayerInput playerInput;
    private PlayerInputActions inputActions;
    private void Awake()
    {
        rigidbody = GetComponent();
        playerInput = GetComponent();
        inputActions = new PlayerInputActions();
        inputActions.Player.Enable();//确保实例化的PlayerInputActions起作用,不然Awake结束后,该实例就被销毁了。
        inputActions.Player.Jump.performed += Jump;

        //重新绑定事件
        inputActions.Player.Disable();//先释放
        inputActions.Player.Jump.PerformInteractiveRebinding().OnComplete(
            callback =>
            {
                Debug.Log(callback);
                callback.Dispose();
                inputActions.Player.Enable();
            }
            ).Start();

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/732913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存