网站建设的需求是什么意思,宝山网站推广,网站开发系统需求说明书,品牌vi设计公司企业开始学的是 Input Manager 比较好理解#xff0c;Input System却不好理解#xff0c;教程也找了很多#xff0c;感觉都讲的不清楚#xff0c;我这里做一个最简单的用 Input System 添加鼠标左键和右键的效果。
1. 安装 Input System 包
首先这个功能不是内置的#xff0…开始学的是 Input Manager 比较好理解Input System却不好理解教程也找了很多感觉都讲的不清楚我这里做一个最简单的用 Input System 添加鼠标左键和右键的效果。
1. 安装 Input System 包
首先这个功能不是内置的需要自行安装。打开Unity上方工具栏中的 Window →Package Manager打开Unity插件包管理界面 Package Manager是Unity的包管理工具可以安装、卸载、升级相关的包。左上角选择 Unity Registry在输入框搜索到结果之后点击右下角的安装即可安装好之后会有一个绿色的钩。
2. 启用 Input System
系统默认使用 Input ManagerOld 在 Edit → Project settings → Player中 之后unity会重启。
3. 新建配置文件
在一个场景中Asset目录新建 Setting目录放置项目的配置文件。然后右键 Create → Input Actions。我改名叫 InputActions。 4. 配置事件集合
选中刚刚新建的 InputActions如上图点击Edit Asset 点击左上角选择Add Control Schema然后新建一个Actions Map可以理解为一大组事件的集合方便可以灵活切换然后新建 Actions比如我新建的LeftClick和RightClick表示鼠标左右键点击。 点击后边的加号选择 Add binding添加事件绑定。 在Path里选择Mouse鼠标- Left Button
5. 生成C#文件 勾选然后点击Apply即可。
6. 添加游戏物体
添加一个游戏物体然后添加组件 PlayerInputActions选择我们新建的那个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键);}}
}