Unity基础入门分享Unity游戏开发经验分享

Unity InputSystem 实战(一)

2023-05-21  本文已影响0人  能不写代码么

介绍

本篇文章实战基于Unity2023.1.0a26版本:1.5版本官网地址
Unity推出的新版的输入系统,旧版输入系统很大概率要弃用了,所以抓紧来学习下吧

注意:新的输入系统需要Unity 2019.4+和.NET 4运行时。它在使用旧的.NET 3.5运行时的项目中不起作用


安装方式


InputActions创建


InputActions设置

Value 主要用于状态连续更改的输入,例如鼠标的移动,手柄的遥感。如果有多个设备绑定这个Action,只会发送其中一个设备(最受控制的)的输入
Button 用于每次按下时触发的Action
Pass-Through 和Value一样,区别在于如果有多个设备绑定这个Action,会发送所有设备的输入
binding 给Action添加binding的设备操作
Add Binding 普通的绑定,可以绑定一个按钮,光标,遥杆等
Add Up\Down\Left\Right Composite 四个按钮的组合,返回值为Vector2,例如WASD
Add Binding With One Modifier 需要同时按下两个按钮的组合,例如ctrl+1
Add Bingding With Two Modifiers 需要同时按下三个按钮的组合,例如shift+ctrl+1

代码调用 InputAction

using UnityEngine;

public class InputManager : MonoBehaviour
{
    public static GameControls gameControls { get; private set; }

    private void OnEnable()
    {
        if (gameControls == null)
            gameControls = new GameControls();

        gameControls.Enable();
    }

    private void OnDisable()
    {
        if (gameControls != null)
            gameControls.Disable();
    }
}

旧版代码控制人物上下左右移动

using UnityEngine;

public class Player : MonoBehaviour
{
    [Header("主角移速")]
    public float moveSpeed;

    private Rigidbody2D playerRB;

    // 缓存移动方向
    private float input_X;
    private float input_Y;
    private Vector2 input_MoveDir;

    private void Awake()
    {
        playerRB = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        PlayerInput();
    }

    private void FixedUpdate()
    {
        Move();
    }

    private void PlayerInput()
    {
        input_X = Input.GetAxisRaw("Horizontal");
        input_Y = Input.GetAxisRaw("Vertical");
        if (input_X != 0 && input_Y != 0)
        {
            input_X = input_X * 0.7f;
            input_Y = input_Y * 0.7f;
        }
        input_MoveDir = new Vector2(input_X, input_Y);
    }

    private void Move()
    {
        playerRB.MovePosition(playerRB.position + input_MoveDir * moveSpeed * Time.deltaTime);
    }
}

新版代码控制人物上下左右移动

Phase Description
Disabled 操作已禁用,无法接收输入
Waiting 操作已启用,并且正在积极等待输入
Started 输入系统已接收到启动与动作交互的输入
Performed 已完成与操作的交互并持续监听
Canceled 已取消与操作的交互
using UnityEngine;

public class Player : MonoBehaviour
{
    [Header("主角移速")]
    public float moveSpeed;

    private Rigidbody2D playerRB;

    // 缓存移动方向
    private float input_X;
    private float input_Y;
    private Vector2 input_MoveDir;

    private void Awake()
    {
        playerRB = GetComponent<Rigidbody2D>();
    }

    void Start()
    {
        InputManager.gameControls.Player.Move.performed += OnMovePerformed;
    }

    private void OnDisable()
    {
        InputManager.gameControls.Player.Move.performed -= OnMovePerformed;
    }

    private void OnMovePerformed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    {
        Vector2 moveDir = obj.ReadValue<Vector2>();
        input_X = moveDir.x;
        input_Y = moveDir.y;
        if (input_X != 0 && input_Y != 0)
        {
            input_X = input_X * 0.7f;
            input_Y = input_Y * 0.7f;
        }
        input_MoveDir = new Vector2(input_X, input_Y);
    }

    private void FixedUpdate()
    {
        Move();
    }

    private void Move()
    {
        playerRB.MovePosition(playerRB.position + input_MoveDir * moveSpeed * Time.deltaTime);
    }
}
using System;
using UnityEngine;

public class Player : MonoBehaviour
{
    [Header("主角移速")]
    public float moveSpeed;

    private Rigidbody2D playerRB;

    // 缓存移动方向
    private float input_X;
    private float input_Y;
    private Vector2 input_MoveDir;

    private void Awake()
    {
        playerRB = GetComponent<Rigidbody2D>();
    }

    void Start()
    {
        InputManager.gameControls.Player.Move.performed += OnMovePerformed;
        InputManager.gameControls.Player.Move.canceled += OnMoveCanceled;
    }

    private void OnDisable()
    {
        InputManager.gameControls.Player.Move.performed -= OnMovePerformed;
        InputManager.gameControls.Player.Move.canceled -= OnMoveCanceled;
    }

    private void OnMovePerformed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    {
        Vector2 moveDir = obj.ReadValue<Vector2>();
        input_X = moveDir.x;
        input_Y = moveDir.y;
        if (input_X != 0 && input_Y != 0)
        {
            input_X = input_X * 0.7f;
            input_Y = input_Y * 0.7f;
        }
        input_MoveDir = new Vector2(input_X, input_Y);
    }

    private void OnMoveCanceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    {
        input_X = 0;
        input_Y = 0;
        input_MoveDir = Vector2.zero;
    }

    private void FixedUpdate()
    {
        Move();
    }

    private void Move()
    {
        playerRB.MovePosition(playerRB.position + input_MoveDir * moveSpeed * Time.deltaTime);
    }
}

动画演示(Gif总是不播放,可以看我其他博客地址)

使用技巧

    using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
    private void Update()
    {
        foreach (var touch in Touch.activeTouches)
            Debug.Log($"{touch.touchId}: {touch.screenPosition},{touch.phase}");
    }
上一篇下一篇

猜你喜欢

热点阅读