键盘相关的输入函数:
1. public static bool GetKey(string name);
描述:
当用户保持某个键(name)为按下状态时返回true。比如自动发射。
要处理输入的时候推荐使用Input.GetAxis 和 Input.GetButton, 因为它们允许终端用户自定义键的名称。
if (Input.GetKey("up"))
if (Input.GetKey("down"))
public static bool GetKey(KeyCodekey);
描述:
当用户保持某个由KeyCode定义的键为按下状态时返回true。
if (Input.GetKey(KeyCode.UpArrow))
if (Input.GetKey(KeyCode.DownArrow))
2. public static boolGetKeyDown(stringname);
描述:
当用户开始按下由name定义的键时返回true。
你需要在Update函数中调用这个函数,因为按键状态在每一帧被重置。如果用户没有释放按键并重新按下它,函数将不 会返回true。
public static boolGetKeyDown(KeyCodekey);
3. public static boolGetKeyUp(string name);
描述:
当用户释放由name定义的键时返回true。
public staticboolGetKeyUp(KeyCodekey);
总结:GetKeyDown 和 GetKeyUp 分别代表了按键的down和up状态,而hold状态则使用了函数GetKey。string类型的参数name表示键盘上每一个键项对应的名字,并使用小写(比如空格键--space, 向上方向键--up,A键--a)。
鼠标相关的输入函数:
1. public static bool GetMouseButton(int button);
描述:
当指定的鼠标键保持按下状态时返回true。
button的值表示: 0 左键, 1右键, 2 滚轮键。
if(Input.GetMouseButton(0))
Debug.Log("Pressed left click.");
if(Input.GetMouseButton(1))
Debug.Log("Pressed right click.");
if(Input.GetMouseButton(2))
Debug.Log("Pressed middle click.");
2. public static bool GetMouseButtonDown(int button);
描述:
指定的键按下时返回true。
3. public static bool GetMouseButtonUp(int button);
描述:
指定的键释放时返回true。
总结:GetMouseButtonDown 和 GetMouseButtonUp 分别代表了鼠标键的down 和 up 状态,而hold 状态则使用函数GetMouseButton 。 参数 button 用整数型来表示按下哪个键:0 左键, 1 右键, 2 滚轮键。
自定义轴(Axes)相关的输入函数:
1. public static bool GetButton(string buttonName);
描述:
当由buttonName 定义的虚拟键保持按下状态时返回true。
只有当执行的事件触发一个动作时使用这个函数。当输入控制持续的变化时使用GetAxis
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
publicGameObject projectile;
public floatfireRate = 0.5F;
private floatnextFire = 0.0F;
void Update() {
if(Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
GameObject clone = Instantiate(projectile, transform.position,transform.rotation) as GameObject;
}
}
}
2. public static bool GetButtonDown(string buttonName);
描述:
当由buttonName定义的虚拟键被按下时返回true。
3. public static boolGetButtonUp(string buttonName);
描述:
当由buttonName定义的虚拟键被释放时返回true
4. public static float GetAxis(string axisName);
描述:
返回由axisName定义的虚拟轴的值。
如果是键盘或者操作杆,返回值在 -1 到 1 之间。如果是鼠标的移动,则返回值时敏感性(sensitivity)的倍数,其范围不是 -1 到 1。
总结: 如果要获得虚拟键的down , up, hold状态,使用的函数分别是 GetButtonDownGetButtonDown ,GetButtonUp, GetButton 。 如果要获得的值是动态的,则使用GetAxis。参数都是自定义的键名。