Unity 基础 - Input 类
来源:互联网 发布:单片机控制系统 编辑:程序博客网 时间:2024/05/10 16:25
任何一款游戏都必须和用户进行交互才行,最常用的就是通过键盘和鼠标进行交互,在 Unity
中想要获取用户的键盘或鼠标的事件的话,就必须使用 Input
类来获取。
打开 Unity ,新建一个空的项目,创建一个 Cube 物体:
然后给这个 Cube 物体添加一个新脚本:
创建完成后,双击打开此脚本,就需要写一些代码了,如果想要获取用户按键或鼠标点击的事件,那就需要使用 Input
类。一般来说 Input
类的方法需要写在 Update()
方法中。
using UnityEngine;using System.Collections;public class CubeScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame // 获取用户的键盘和鼠标点击事件需要用到 Input 类 void Update () { // 每帧都需要监听用户的事件 // 在当前这一帧中,如果用户按下了 W 就会返回 true,否则返回 false bool keyCodeDown = Input.GetKeyDown (KeyCode.W); // 检测键盘按下 if (keyCodeDown) { // print 只能在 MonoBehaviour 的子类中使用,其他情况只能使用 Debug.Log() 输出 print ("往前走"); } if (Input.GetKeyDown (KeyCode.S)) { Debug.Log("往后退"); } // 检测键盘弹起 if (Input.GetKeyUp (KeyCode.Alpha1)) { Debug.Log("按下 1 键,弹起"); } //按下鼠标左键 if (Input.GetMouseButtonDown (0)) { print ("按下鼠标左键"); } //按下鼠标右键 if (Input.GetMouseButtonDown (1)) { print ("按下鼠标右键"); } //按下鼠标中键 if (Input.GetMouseButtonDown (2)) { print ("按下鼠标中键"); } }}
点击运行按钮, 按下 W 之后,如下图输出
则说明已经成功的接收到了用户按下键盘的事件。
0 0
- Unity 基础 - Input 类
- Unity基础——Input
- Unity 入门 Input 类
- unity ------- input
- Unity Input
- Unity学习(八)Input类
- Unity API(二):Input类
- Unity基础四个类
- Unity Input 鼠标模拟
- Unity Input Touch
- unity Input Touch
- Unity transform和Input
- Unity Transform和Input
- Unity Mobile Device Input
- unity的input鼠标
- Unity Input Touch fingerId
- unity Input.GetAxis()函数
- 蓝鸥Unity开发教程之课时7 Input类
- 领域驱动/DDD模型初识
- firewall-config保持配置的方法
- iOS通过dSYM文件分析crash
- 【安卓篇】安卓Activity进程被杀规则
- ThinkAndroid简介
- Unity 基础 - Input 类
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- Android 设备上装两个相同的 App
- Windows下github使用说明
- Shell判断功能与比较符号
- spring boot(4) 权限
- MySQL命令行导出数据库
- 实现ScrollView滑动时标题栏的隐藏效果
- iOS9 视频播放器 AVPlayerViewController的简单使用