InputSystem学习记录

安装

image.png

概念

一、创建 Input Actions

image.png

Input Actions概念及结构关系

在我们InputSystem中所用到结构关系为
InputSystem=>Input Actions=>Action Maps=>Actions

ActionMaps (表)

简单来说我们可以将InputActions视为我们项目里其中一个控制器的输入操作管理集,而ActionMaps则为该控制器其中的一个输入映射集。
9045e468c0c4075b27e3e31c2177ebef_f1c9f0648cd14deaa9467224e684cace.png

image.png

Actions (动作)

而Actions则为ActionMaps里其中一个动作输入映射

ActionProperties (值类型)

在Actions中也有许多参数,其中ActionType则是我们最常用到。其概念为我们该动作输入映射的类型,有以下三种类型

1.Button 默认设置,包括按钮或者按键触发的一次性动作
2.Value 提供一种连续状态变化事件,如果设置了多个输入,就会切换到最主要的一个。用它进行模拟控制,比如移动。
3.Pass Through 和Value很相似,但它不会像Value一样(如果有多个输入时,不会只选择最主要的那个,而把其他的输入忽略)

在使用Value或者Pass Through Types时,你会看到一个额外的选项 Control Type为该Value的返回值类型

二、获取输入

1.直接读取装置状态

这是最简单、最直接的工作流程,但最不灵活。如果您想使用一种类型的设备快速实现,这将非常有用。如果要为用户提供多种类型的输入,或者要面向多个平台,则它可能不是最佳选择。
可以通过引用设备的控件并读取它们当前生成的值,使用如下代码直接从连接的设备读取值:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Test1 : MonoBehaviour
{
    Gamepad gamepad; //手柄
    Keyboard keyboard; //键盘
    Mouse mouse; //鼠标
    Pointer pointer; //指针

    void Start()
    {
        gamepad = Gamepad.current; //手柄
        keyboard = Keyboard.current; //键盘
        mouse = Mouse.current; //鼠标
        pointer = Pointer.current; //指针
    }

    // Update is called once per frame
    void Update()
    {
        if (gamepad != null)
        {
            Debug.Log(gamepad.leftStick.ReadValue());//手柄遥感的偏移
            if (gamepad.bButton.wasPressedThisFrame)
                Debug.Log("按下B键");
        }
        
        if (keyboard != null)
        {
            //执行顺序 isPressed = false ->
            //按下:wasPressedThisFrame = true ->
            //中途:isPressed = true ->
            //松开:wasReleasedThisFrame = true ->
            //结束 isPressed = false
            if (keyboard.wKey.wasPressedThisFrame)
                Debug.Log("w键按下(一直按住w键的话,也只执行一次)");
            if (keyboard.wKey.wasReleasedThisFrame)
                Debug.Log("w键松开");
            Debug.Log("是否按住w键:" + keyboard.wKey.isPressed);
        }
        
        
        if (mouse != null)
        {
            Debug.Log(mouse.scroll.ReadValue());//滚轮的滚动值,向前滚Y的值为正,向后滚为负
 
            if (mouse.leftButton.wasPressedThisFrame)
                Debug.Log("按鼠标左键");
            if (mouse.rightButton.wasPressedThisFrame)
                Debug.Log("按鼠标右键");
            if (mouse.middleButton.wasPressedThisFrame)
                Debug.Log("按滚轮键");
        }
        
        
        if (pointer != null)
        {
            Debug.Log("位移量 "+pointer.delta.ReadValue());//与上一帧的偏移
            Debug.Log("在空间中的坐标 "+pointer.position.ReadValue());//在空间中的坐标
        }
    }
}

image.png
image.png

2. 使用嵌入式操作

在这里插入图片描述
image.png

image.png

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class test2 : MonoBehaviour
{
    public InputAction MoveAction;

    public InputAction JunpAction;


    private void Start()
    {
        JunpAction.performed += Junp;
    }

    private void Junp(InputAction.CallbackContext obj)
    {
        Debug.Log("跳了");
    }

    void Update()
    {
        // 读取每帧 的值
        Vector2 moveAmount = MoveAction.ReadValue<Vector2>();
        Debug.Log(moveAmount);
    }
    
    //操作必须启用 禁用
    public void OnEnable()
    {
        MoveAction.Enable();
        JunpAction.Enable();
    }

    public void OnDisable()
    {
        MoveAction.Disable();
        JunpAction.Disable();
    }
}

image.png

3.资产调用

3.1 在检查器中引用动作资源

要通过检查器参考使用动作资源:

  1. 在脚本中创建一个字段。public InputActionsAsset
  2. 在检查器中指定引用
  3. 使用字符串按名称访问脚本中的操作。

image.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class test3 : MonoBehaviour
{
    //表
    public InputActionAsset actions;
    //动作
    private InputAction moveAction;
    void Start()
    {
        // 获得move映射
        moveAction = actions.FindActionMap("PC").FindAction("Move");

        // 订阅跳方法
        actions.FindActionMap("PC").FindAction("Junp").performed += OnJump;
    }

    private void OnJump(InputAction.CallbackContext obj)
    {
        //跳
    }


    void Update()
    {
        //得到输入值
        Vector2 moveVector = moveAction.ReadValue<Vector2>();
    }
    
    void OnEnable()
    {
        actions.FindActionMap("PC").Enable();
    }
    void OnDisable()
    {
        actions.FindActionMap("PC").Disable();
    }
}

3.2 通过 C# 包装器引用操作资产

若要通过 C# 包装器使用操作资源,请执行以下操作:

  1. 在项目窗口中选择您的动作资源
  2. 在检查器中,启用**“生成 C# 类**”,然后选择**“应用**”。应在项目窗口中看到与操作资源同名的 C# 资源。
  3. 在脚本中创建操作 C# 类的实例。
  4. 使用操作 C# 类的 API 访问脚本中的操作。

image.png

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class test4 : MonoBehaviour
{
    private AAA Actions;


    private void Start()
    {
        //创建实例
        Actions = new AAA();
        //订阅跳方法
        Actions.PC.Junp.performed += OnJump;
    }

    private void OnJump(InputAction.CallbackContext obj)
    {
        Debug.Log("跳");
    }
    
    void Update()
    {
        // 读取每帧 的值
        Vector2 moveAmount = Actions.PC.Move.ReadValue<Vector2>();
        Debug.Log(moveAmount);
    }
    
    void OnEnable()
    {
        Actions.PC.Enable();
    }
    void OnDisable()
    {
        Actions.PC.Disable();
    }
    
}

4.播放器输入组件(Player Input)

4.1 Send Messager(通知)

选择这个模式,这个物体的脚本添加生命周期方法。会自动调用这个方法

image.png
image.pngimage.png

4.2 Broadcast Messages (广播)

一样使用周期方法调用
子物体挂载也能触发(通知肢体发生动作?

image.png
image.pngimage.png

4.3 Invoke Unity Events (事件)

image.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class test6 : MonoBehaviour
{
    public void Jump(InputAction.CallbackContext context)
    {
        Debug.Log("Jump:");
    }

    public void Move(InputAction.CallbackContext context)
    {
        Debug.Log("Move:"+context.ReadValue<Vector2>());
    }
}

image.png

会有三次调用
分别是 Started 开始 、和回调 performed 正在执行 、 canceled 结束

只执行一次

添加参数

image.png
image.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class test6 : MonoBehaviour
{
    public void Jump(InputAction.CallbackContext context)
    {
        if (context.phase == InputActionPhase.Performed)
        {
            Debug.Log("Jump:"+context.ReadValueAsButton());
        }
        
    }

    public void Move(InputAction.CallbackContext context)
    {
        if (context.phase== InputActionPhase.Performed)
        {
            Debug.Log("Move:"+context.ReadValue<Vector2>());
        }
        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值