[Unity] Player02-创建和配置新的输入系统

168 阅读1分钟
1.安装设置
  • 1.打开配置 edit-project settings-player-other settings

image.png

  • 2.使用新的操作系统,确认后会重启

image.png

  • 3.重启后打开包管理器, 搜索Input System,安装

image.png

2.添加设置 (只是演示怎么添加,后面会删掉)
  • 1.创建输入系统,双击打开配置

image.png

image.png

  • 2.设置控制

image.png

  • 3.快速添加上下左右,删掉上面那个no bind

image.png

image.png

3.选择角色对象。添加角色控制 -- 最后也是删掉,用代码来写

PlayerInputControl (名字记得不要写成别的,后面要用到) image.png

image.png

注意, 后面虽然会删掉组件, 但是这里创建的Gameplay不会消失,同时名字改成Gameplay image.png

文件自动创建好了

image.png

  • 删掉组件

image.png

4.使用c#脚本来控制角色
#注意: 这里有两个C#脚本, 一个是手动创建的在:
Assets/Script/PlayerController.cs, 
#一个是由Input Actions文件自动生成的在:
Assets/InputSystem/PlayerInputControl.cs

然后在第一个文件中编写控制代码,引用第二个文件, 第二个文件代码是自动生成不用改
  • 1.首先创建一个C#脚本用来控制人物 Assets/Script/PlayerController.cs

  • 2.创建角色输入文件(使用添加组件的方式,和上面一样,创建完之后删掉)

image.png

image.png

image.png

image.png

image.png

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

public class PlayerController : MonoBehaviour
{
    public PlayerInputControl inputControl;
    public Vector2 inputDirection;


    private void Awake()
    {
        inputControl = new PlayerInputControl();
    }


    private void OnEnable()
    {
        inputControl.Enable();
    }

    private void OnDisable()
    {
        inputControl.Disable();
    }

    private void Update()
    {
        // 这个Gameplay是添加Player Input时创建的,改名的,虽然后面删除了组件
        inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
    }
}

可以在这里打开控制的map

image.png