01_Unity3D的输入(Input)——键盘和鼠标
来源:互联网 发布:java分布式缓存技术 编辑:程序博客网 时间:2024/06/07 16:49
Unity3D使用input类控制用户的输入,输入包括了用户键盘,鼠标,触摸,重力感应以及地理位置输入等输入方式。
本节我们介绍Unity3D中如何监听用户的鼠标、键盘输入。
首先我们介绍一个很重要的类KeyCode,此类中的值记录各种按键,详细键码参照Unity圣典:http://game.ceeger.com/Script/Enumerations/KeyCode/KeyCode.html。
一.键盘的输入
•GetKey,GetKeyDown,GetKeyUp三个方法分别获取用户键盘按键的输入
GetKey:用户长按按键有效;
GetKeyDown:用户按下按键时有效;
GetKeyUp:用户抬起按键时有效;
示例:
public class Test1 : MonoBehaviour {void Update () { bool down = Input.GetKeyDown(KeyCode.Space); bool held = Input.GetKey(KeyCode.Space); bool up = Input.GetKeyUp(KeyCode.Space); if(down) { GUI.Label(new Rect(100,100,400,100),"down"); } else if(held) { GUI.Label(new Rect(100,100,400,100),"held"); } else if(up) { GUI.Label(new Rect(100,100,400,100),"up"); } else { GUI.Label(new Rect(100,100,400,100),"else"); } }}
• anyKey,anyKeyDown
anyKey用于监听用户的任意按键输入或鼠标点击,事件触发后返回true。anyKeyDown:当任意按键松开时返回true。
示例:
public class Test2 : MonoBehaviour { void OnGUI(){ if(Input.anyKey){ GUI.Label(new Rect(100,100,400,100),"测试任意按键"); } }}
•GetButton(string buttonName)
GetButton用于监听Button的输入,参数为自己指定的button的名字,在Unity中可以使用管理器添加或修改Input参数。unity已经为用户预设了多种输入情况,在button中预设了Fire、Jump等输入情况。用户也可以自行修改,打开Edit-->Project Settings-->Input可以看到预设如下:
以Fire1为例,可以看到使用左ctil键或者鼠标左键控制Fire1按钮。
示例:
public class Test3 : MonoBehaviour { void OnGUI(){ if(Input.GetButton("Fire1")){ GUI.Label(new Rect(100,100,400,100),"测试按钮点击"); } }}
另外还有GetButtonDown( 当某个按钮被按下的那一帧返回true)
GetButtonUp( 当某个按钮弹起时返回true)两个方法,就不详细介绍了。
二.鼠标的输入
•mousePosition
Input.mousePosition返回当前鼠标的位置,这里指的是距离原点的像素位置,说明一下,Unity中的原点(0,0)位置为左下角,上位y轴正方向,右为x轴正方向。返回值类型为Vector3。
示例:
public class mono2 : MonoBehaviour { void OnGUI(){ GUI.Label(new Rect(100,100,400,100),"x="+Input.mousePosition.x+" y="+Input.mousePosition.y+" z="+Input.mousePosition.z); }}
当用户移动鼠标是即可显示x,y坐标,z为0。
•GetMouseButton
GetMouseButton 对应的键处于按下状态时返回true
GetMouseButtonDown 对应的键被按下时返回true
GetMouseButtonUp 对应的键弹起时返回true
示例:
public class mono2 : MonoBehaviour { void OnGUI(){<span style="white-space:pre"></span> GUI.Label(new Rect(100,100,400,100),"X="+Input.GetAxis("Fire1")+"Y="+Input.GetAxis("Fire1")); if(Input.GetMouseButton(0)){ GUI.Label(new Rect(100,100,400,100),"鼠标左键按下"); }else if(Input.GetMouseButton(1)){ GUI.Label(new Rect(100,100,400,100),"鼠标右键按下"); }else if(Input.GetMouseButton(2)){ GUI.Label(new Rect(100,100,400,100),"鼠标中键按下"); } }}
在例子中可以看出参数为int型,含义为:0左键,1右键,2中键。
- 01_Unity3D的输入(Input)——键盘和鼠标
- 02_Unity3D的输入(Input)——移动设备输入
- 03_Unity3D的输入(Input)——移动设备重力感应
- 04_Unity3D的输入(Input)——移动设备方向
- linux input subsystem虚拟键盘和鼠标
- 9.2、Libgdx的输入处理之鼠标、触摸和键盘
- Windows编程 DirectInput 鼠标和键盘的输入
- C# webbrowser文本框的键盘和鼠标模拟输入和模拟鼠标点击
- DirectX11 输入设备——DirectInput检测鼠标、键盘状态
- winio模拟键盘鼠标的输入
- GUI——鼠标和键盘事件
- 鼠标和键盘的按键
- 键盘和鼠标的操作
- 鼠标和键盘的故事
- DirectX游戏编程入门——第二部分(游戏编程工具箱) —— 键盘和鼠标输入
- linux 虚拟输入设备(uinput)模拟鼠标和键盘的使用方法
- HTML中input只能输入数字(键盘抬起和粘贴事件)
- input键盘控制输入值
- GDI绘制填充图二
- Wireshark基本介绍和学习TCP三次握手
- UVA 993
- VS2013各版本激活密钥
- linux下Apache简介
- 01_Unity3D的输入(Input)——键盘和鼠标
- uva 138
- 栈
- 互联网时代:免费的是最贵的
- 玩转redis在Java中的使用之(一)redis的安装
- 世界上最好的寿司,不但能吃,还能喝出鸡汤味
- iOS程序的生命周期
- 从setContentView谈起
- Wallis公式及其应用