Unity——New Input System Input System

1. Install

Install:

        Go directly to the package manager Window > Package Manager to install it. After installation, it prompts that a restart is required, and it can be used after restarting.

Notice:

        Set Active Input Handling to Input System in the Player settings in Project Settings.

        It is necessary to change the Standalone Input Module in the EventSystem in the default scene to the new Input System UI Input Module component.

2. use

1. create

Two ways can be created:

1. Right-click Create-->Input Actions to create 

2. After mounting the Player Input component on the object you want to control, click Create Actions to create it.

2. Enter the configuration interface to create Action

If you want to move, jump and other actions, you can double-click the created Input Actions or select this file and click Edit asset

 Enter the configuration interface

 Click the + sign to create ActionMaps, Actions and bind buttons

3. use

1. The namespace that needs to be referenced is using UnityEngine.InputSystem;

2. Use wasd to move

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayController : MonoBehaviour
{
    [SerializeField]
    private InputActionReference moveInput;//绑定PlayerMove/Move
    
    private void OnEnable()
    {
        moveInput.action.Enable();
        
    }
    private void OnDisable()
    {
        moveInput.action.Disable();
    }

    private void Update()
    {
        //检测是否按下wasd键
        //方法1:
        if (moveInput.action.WasPressedThisFrame())
        {
            Debug.Log("按下");
        }
        if (moveInput.action.WasReleasedThisFrame())
        {
            Debug.Log("抬起");
        }
        if (moveInput.action.WasPerformedThisFrame())
        {
            Debug.Log("按下+抬起");
        }

        //方法2:
        /*moveInput.action.performed += ctx =>
        {
            //移动
            transform.Translate(ctx.ReadValue<Vector2>());
        };*/
    }
}

3. Mount the code to the corresponding gameObject

 

Supplement: Replacement of custom buttons (jump)

 1. Jump

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

public class JumpManager : MonoBehaviour
{
    [SerializeField]
    public InputActionReference jumpInput;
    private void OnEnable()
    {
        jumpInput.action.Enable();
    }

    private void OnDisable()
    {
        jumpInput.action.Disable();
    }

    private void Update()
    {
        if(jumpInput.action.WasPressedThisFrame())
        {
            Debug.Log("jump");
        }
    }
}

2. Replace the button

using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;

public class RebindButtons : MonoBehaviour
{
    public InputActionReference jump;
    public JumpManager jumpManager;
    
    public Button jumpButton;
    public TextMeshProUGUI text;

    void Awake()
    {
        jump = jumpManager.jumpInput;
    }
    private void Start()
    {
        jumpButton.onClick.AddListener(ReBinding);
    }

    //更改按键
    private void ReBinding()
    {
        Debug.Log("-----");
        jump.action.Disable();
        var rebind = jump.action.PerformInteractiveRebinding(0)
            .OnCancel(
                    operation =>
                    {
                        jump.action.Enable();
                    })
             .OnComplete(
                    operation =>
                    {
                        Debug.Log("OnComplete");
                        jump.action.Enable();
                    });
        rebind.Start();
    }
}

(Steps to replace the button: click the button, select and press the selected button, click the button just replaced to execute)

Guess you like

Origin blog.csdn.net/weixin_45274937/article/details/128675272