Input.GetKeyDown 获取键按下

static function GetKeyDown (name : string) : bool

Description描述

Returns true during the frame the user starts pressing down the key identified by name.

当用户按下指定名称的按键时的那一帧返回true。

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

你需要在Update方法中调用这个方法,此后每一帧重置状态时,它将不会返回true除非用户释放这个按钮然后重新按下它

For the list of key identifiers see Input Manager. When dealing with input it is recommended to use Input.GetAxis and Input.GetButton instead since it allows end-users to configure the keys.

按键的标识符列表可以在输入管理器中找到,当处理输入时推荐使用Input.GetAxis和Input.GetButton,它们允许最终用户自定义按键。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Update() {
		if (Input.GetKeyDown("space"))
			print("space key was pressed");

	}
}
function Update () {
	if (Input.GetKeyDown ("space"))
		print ("space key was pressed");
}

• static function GetKeyDown (key : KeyCode) : bool

Description描述

Returns true during the frame the user starts pressing down the key identified by the key KeyCode enum parameter.

当用户按下映射到指定枚举参数的按键时的那一帧返回true。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Update() {
		if (Input.GetKeyDown(KeyCode.Space))
			print("space key was pressed");

	}
}
function Update () {
	if (Input.GetKeyDown (KeyCode.Space))
		print ("space key was pressed");
}
最后修改:2011年3月12日 Saturday 19:38

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。