文件管理
创建代码文件夹,创建人物板块,创建c#脚本,等待自动编译
2.代码添加到人物中(或拖拽)

3.添加后双击进入代码编辑器
4.unity升级为新的input system
新的Input System可以更方便地处理不同的输入设备,并根据项目需求进行配置和映射,提高游戏的灵活性和适应性
①

②
在下边active input handling中选择new的那个,apply,unity自动重启
③
找到这个new package,在window->package manager中

④
点击下边install,等待安装
⑤
设置new input system的配置文件的例子
1.在文件中创建输入系统文件夹

法一 手动配置input action配置表
2.里面添加input actions,在最最下边,创建,即为上图中的input ctrl文件

3.双击input ctrl文件打开配置窗口,可以添加需要检测的按键
action map中的不同条件可以在进行切换时候转化成不同的操作内容(旁边的actions)

例如这样设置一个map叫gameplay,他对应的动作叫movement,动作类型value,控制类型二维向量
点击该movement action后边的加,添加上下左右组合,可以通过对这四个完成按键的设置
这里的按键设置时的监听,连着什么输入设备就监听什么,手柄键盘之类

可以通过添加配置表来区分输入设备,方便快速筛选显示,键盘按键不会被区分,但是手柄配置可以


//(删掉这个示例用的input ctrl文件了)
法二 用unity自动生成默认的input action配置表
为小人添加Player Input组件(这是new input system下的组件,可以完成人物基本的输入配置)
将这个文件放在input system文件夹下,创建,可以看见这个文件已经有了基本的按键配置

在这里完成配置

删除
法三 代码完成配置
1.对这个input actions文件添加c#文件(打勾,apply)

创建结果如下

2.在小人身上挂着的脚本上加入这个类的实例,如此可以调用这个类中的方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
//添加新输入系统的库如上
public class PlayerController : MonoBehaviour
{
public PlayerInputControl inputControl;
//进入游戏,创建实例
private void Awake()
{
inputControl = new PlayerInputControl();
}
//当前物体被启用时调用onenable()
private void OnEnable()
{
inputControl.Enable();
}
//然后start
//物体关闭,ondisable
private void OnDisable()
{
inputControl.Disable();
}
}
3.实现读取移动的数值
也就是读取这个二维向量,数值在PlayerInputControl实例下的map下的action中,调用读取数值方法,读取二维向量,传入到新创建的public Vector2 inputDirection中

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
//添加新输入系统的库如上
public class PlayerController : MonoBehaviour
{
public PlayerInputControl inputControl;
//创建public变量,可以显示在人物的inspector中
public Vector2 inputDirection;
//进入游戏,创建实例
private void Awake()
{
inputControl = new PlayerInputControl();
}
//当前物体被启用时调用onenable()
private void OnEnable()
{
inputControl.Enable();
}
//然后start
//物体关闭,ondisable
private void OnDisable()
{
inputControl.Disable();
}
//刷新时读取value
private void Update()
{
inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
}
}
可以看到play后按上下左右后inputDirection变量会变化

本文详细介绍了如何在Unity中使用新InputSystem进行代码管理和配置,包括手动创建和编辑inputactions,使用PlayerInput组件以及通过C#代码实现输入值的读取,以提升游戏的灵活性和适应性。
3870

被折叠的 条评论
为什么被折叠?



