Unity Input System最简单使用

开始学的是 Input Manager 比较好理解,Input System却不好理解,教程也找了很多,感觉都讲的不清楚,我这里做一个最简单的用 Input System 添加鼠标左键和右键的效果。

1. 安装 Input System 包

首先这个功能不是内置的,需要自行安装。打开Unity上方工具栏中的 WindowPackage Manager打开Unity插件包管理界面, Package Manager是Unity的包管理工具,可以安装、卸载、升级相关的包。左上角选择 Unity Registry,在输入框搜索到结果之后,点击右下角的安装即可,安装好之后,会有一个绿色的钩。
在这里插入图片描述

2. 启用 Input System

系统默认使用 Input Manager(Old) ,在 Edit → Project settings → Player中
在这里插入图片描述
之后unity会重启。

3. 新建配置文件

在一个场景中,Asset目录新建 Setting目录,放置项目的配置文件。然后右键
Create → Input Actions。我改名叫 InputActions。
在这里插入图片描述

在这里插入图片描述

4. 配置事件集合

选中刚刚新建的 InputActions,如上图,点击Edit Asset
在这里插入图片描述
点击左上角,选择Add Control Schema,然后新建一个Actions Map(可以理解为一大组事件的集合,方便可以灵活切换),然后新建 Actions,比如我新建的LeftClickRightClick表示鼠标左右键点击。
在这里插入图片描述
点击后边的加号,选择 Add binding,添加事件绑定。
在这里插入图片描述
在Path里选择Mouse(鼠标)-> Left Button

5. 生成C#文件

在这里插入图片描述
勾选,然后点击Apply即可。

6. 添加游戏物体

添加一个游戏物体,然后添加组件 PlayerInput,Actions选择我们新建的那个InputActions。
在这里插入图片描述

7. 脚本测试

创建一个脚本,并且绑定到游戏对象上,比如我的 InputDemo.cs ,使用我们刚刚第5步自动生成的那个cs文件,运行游戏,点击鼠标即可。

using UnityEngine;
using UnityEngine.InputSystem;

public class InputDemo : MonoBehaviour {
  private InputActions playerInputActions;

  void Awake() {
    playerInputActions = new InputActions();
  }

  private void OnEnable() {
    playerInputActions.UI.Enable();
  }

  private void OnDisable() {
    playerInputActions.UI.Disable();
  }

  private void OnMouseDown() {
    print("down");
  }

  private void Update() {
    // IsPressed会有多次
    if (playerInputActions.UI.LeftClick.IsPressed()) {
      print("点击left键");
    }

    // trigger只执行一次
    if (playerInputActions.UI.RightClick.triggered) {
      print("点击right键");
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一份 Unity Input Manager 的代码示例: ``` using UnityEngine; public class InputManager : MonoBehaviour { public string horizontalAxis = "Horizontal"; public string verticalAxis = "Vertical"; public string jumpButton = "Jump"; public string fireButton = "Fire1"; private void Update() { float horizontal = Input.GetAxis(horizontalAxis); float vertical = Input.GetAxis(verticalAxis); if (Input.GetButtonDown(jumpButton)) { // 处理跳跃事件 } if (Input.GetButtonDown(fireButton)) { // 处理开火事件 } } } ``` 在这份代码中,我们定义了四个字符串变量:`horizontalAxis`,`verticalAxis`,`jumpButton` 和 `fireButton`。这些变量代表着游戏中的四个操作,即左右移动,上下移动,跳跃和开火。 在 `Update` 函数中,我们使用 `Input.GetAxis` 函数获取了横向和纵向轴的值,并使用 `Input.GetButtonDown` 函数处理跳跃和开火事件。 请注意,这份代码仅是一个简单的示例,它可以作为您自己代码的基础。您可以在此基础上添加自己的代码,以实现更复杂的操作。 ### 回答2: Unity中的Input Manager是一个用于管理用户输入的工具,可以通过它来获取玩家的键盘、鼠标或者手柄输入。下面是一个简单Unity Input Manager的代码示例: ```csharp using UnityEngine; public class InputManager : MonoBehaviour { public float moveSpeed = 5f; private Vector3 moveDirection; private void Update() { // 获取用户输入的水平和垂直轴的值 float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); // 设置移动方向 moveDirection = new Vector3(horizontalInput, 0, verticalInput).normalized; // 移动游戏对象 transform.Translate(moveDirection * moveSpeed * Time.deltaTime); } } ``` 在上述代码中,我们创建了一个名为"InputManager"的MonoBehaviour类。在Update方法中,我们使用了`Input.GetAxis`函数来获取玩家输入的水平和垂直轴的值,这里的"Horizontal"和"Vertical"是Unity内置的输入管理器定义的默认轴。获取到用户的输入后,我们使用`normalized`函数将输入向量归一化,保证每次移动长度为1,并设置了移动速度`moveSpeed`来调整移动距离。最后,我们使用`Translate`函数将游戏对象按照移动方向和速度进行移动。 上述代码是一个简单Unity Input Manager代码示例,可以用来获取用户的输入并进行相应的移动操作。根据实际需求,你还可以扩展该代码,例如:添加更多的输入处理逻辑、处理不同平台的输入方式等。 ### 回答3: UnityInput Manager是一个用于管理输入的系统,用于处理玩家的输入设备,如键盘、鼠标和手柄。下面是一个基本的Unity Input Manager的示例代码。 1. 首先,需要创建一个新的C#脚本,命名为"InputManager"。 2. 在该脚本中,需要使用Unity的命名空间`UnityEngine`和`UnityEngine.InputSystem`。 3. 在脚本中添加以下成员变量: ``` private PlayerInput _playerInput; private float _horizontalInput; private float _verticalInput; private bool _jumpInput; ``` 4. 在`Start`方法中,获取`PlayerInput`组件并将其赋值给`_playerInput`变量: ``` private void Start() { _playerInput = GetComponent<PlayerInput>(); } ``` 5. 实现`Update`方法以获取玩家的输入: ``` private void Update() { _horizontalInput = _playerInput.actions["Move Horizontal"].ReadValue<float>(); _verticalInput = _playerInput.actions["Move Vertical"].ReadValue<float>(); _jumpInput = _playerInput.actions["Jump"].ReadValue<float>() > 0; } ``` 6. 在这个例子中,假设已经在Input Manager中设置了名称为"Move Horizontal"和"Move Vertical"的两个输入轴,以及一个名称为"Jump"的输入按钮。 7. 在脚本中可以通过`_horizontalInput`和`_verticalInput`来获取水平和垂直输入的值,在需要使用时可以直接访问这些变量。 8. 同样,可以通过`_jumpInput`来判断玩家是否按下了跳跃按钮。 9. 在场景中的游戏对象上添加该脚本。 以上就是一个基本的Unity Input Manager的示例代码,可以通过这种方式来管理输入设备和获取玩家的输入。当然,实际使用时还可以根据需要进行进一步的扩展和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值