#1.导入包

#2.创建Input Actions

#3.添加输入地图,绑定其中的数据类型及输入设备

#4.使用Input Actions生成C#代码

#5.创建Actions中的对应Action的可实例化回调接口
如对应Move的PlayerInput
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

[CreateAssetMenu(menuName = "Player Input")]//可创建的实例化
public class PlayerInput : ScriptableObject, InputActions.IGameplayActions//继承InputActions中对应输入地图的回调接口
{
public event Action onMove;
public event Action onStopMove;

InputActions inputActions;
private void OnEnable()//实例化输入,设置此为对应地图回调
{
    inputActions = new InputActions();
    inputActions.Gameplay.SetCallbacks(this);
}
private void OnDisable()//禁用输入
{
    DisableAllInputs();
}
public void DisableAllInputs()
{
    inputActions.Gameplay.Disable();
}

public void EnableGamePlayerInput()//外部启用方法
{
    inputActions.Gameplay.Enable();
    //启用时鼠标隐藏,锁定
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
}
//实现回调接口方法
public void OnMove(InputAction.CallbackContext context)
{
    //根据输入,调用订阅对应事件的方法
    if (context.phase == InputActionPhase.Performed)//按键处于按下状态时
        onMove?.Invoke(context.ReadValue<Vector2>());

    if (context.phase == InputActionPhase.Canceled)//松开按键时
        onStopMove?.Invoke();
}

}
#6.添加角色控制脚本,获取回调接口,订阅其中输入对应的事件,与实现逻辑

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

[RequireComponent(typeof(Rigidbody2D))]//特性添加组件
public class Player : MonoBehaviour
{
[SerializeField] PlayerInput input;//回调接口
Rigidbody2D rig;
[SerializeField] float moveSpeed = 10f;
private void Awake()
{
rig = GetComponent();
}
private void OnEnable()
{
input.onMove += OnMove;
input.onStopMove += OnStopMove;
}
// Start is called before the first frame update
void Start()
{
rig.gravityScale = 0;
input.EnableGamePlayerInput();//激活动作表
}

// Update is called once per frame
void Update()
{
    
}
private void OnMove(Vector2 dir)//根据输入实现逻辑
{
    //Vector2 moveAmount = dir * moveSpeed;
    rig.velocity = dir * moveSpeed;
}
private void OnStopMove()
{
    rig.velocity = Vector2.zero;
}

}
#设置文件

#添加新的Action:Fire

保存后@InputActions中更新IGameplayActions接口中的方法
之后实现PlayerInput继承的IGameplayActions中的方法
//实现回调接口方法
public event Action onFire;
public event Action stopFire;

public void OnFire(InputAction.CallbackContext context)
{
    if (context.phase == InputActionPhase.Performed)//按键处于按下状态时
        onFire?.Invoke();

    if (context.phase == InputActionPhase.Canceled)//松开按键时
        stopFire?.Invoke();
}