[Unity study notes] Basic functions of the new input system

A new input system has been implemented in the new version of Unity. This input system is very convenient and can "record" each input button in different scenarios, and it also takes into account different input systems such as keyboards and gamepads.

Add new input system:

1.Edit->Project Settings

Select Player and change Active Input Handing to Input System Package(New).

Save and wait for Unity to restart.

2.Windows->Package Manger,Additional Input System

After adding a new input system, right click in the project to find the Input Action at the bottom to add a new input system.

1. Action Maps is equivalent to a classification of different scene inputs.

For example, create a new Gameplay in Action Maps, which is dedicated to manipulating character movement and other character-related inputs.

At the same time, add a UI, which is dedicated to the various inputs of the UI.

In this way, different inputs can be classified into categories, which is quite convenient.

2. Actions are the buttons bound to the effect achieved.

For example, I want to use WASD to achieve the function of moving. First, you need to add an Actions list in Actions: Move (click the plus sign at the top). Modify the Action Properties on the right: Action Type = Value; Control Type = Vector2. After that, right-click Move, and the up and down movement keys are automatically added. (I don't know the principle)

In actual use, we don’t need to bind them one by one, we can directly use the key bindings that come with Unity.

First we need to add a component to the character: Player Input.

 Choose Create Actions.

Unity has automatically added the basic key bindings for us.

Then remove the component.

Click Apply, and Unity will automatically generate this Input class for us.

Then you can add scripts to the character, and control the movement of the character through the new input system.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControl : MonoBehaviour
{
    public PlayerInputControl inputControl;

    public Vector2 inputDirection;
    private void Awake() 
    {
        inputControl = new PlayerInputControl();  //实例化一个PlayerInputConrol类
    }

    private void OnEnable()  //组件开启,输入系统随之开启
    {
        inputControl.Enable();
    }

    private void OnDisable()  //组件关闭,输入系统随之关闭
    {
        inputControl.Disable();
    }

    private void Update() 
    {
        inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
        //读取键盘输入的Vector2值
    }

}

Through such code, the direction vector can be read.

In the code, you can add function methods for each Action through the += symbol. For example I want to add a jump function method. I first bind the jump key space in the Input System.

 Then add the function method in Awake of the code.

private void Awake()
{
    inputControl = new PlayerInputControl();
    inputControl.Gameplay.Jump.started += jump;
}
private void jump(InputAction.CallbackContext obj)
{
    throw new NotImplementedException();
}

started refers to the button bound to Jump to trigger the corresponding function method.

This code means that when the system checks that the space is pressed, it executes the jump function method.

You can also use the form of an anonymous function.

private void Awake()
{
    inputControl = new PlayerInputControl();
    inputControl.Gameplay.Jump.started += jump =>
    {
        //函数方法
    };
}

At this point jump can be modified to any name and can be repeated.

=> is an anonymous function symbol. The general meaning is to return the value on the right to the left (if there is a value and the left is a variable).

For example, int x => x*x; is to assign the square of x to x.

end:

The scaleability of the code can be enhanced by some specific codes.

1.[Header("Name")]

The names in this code will appear in the Unity editor for us to read.

For example

[Header("基本参数")]

public Vector2 inputDirection;

Return to the Unity editor

 The basic parameters appear in the code component.

2.region area

Using this code, you can define foldable code segments independently, making the code more concise.

 

 

Guess you like

Origin blog.csdn.net/qq_65021355/article/details/130310149