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");
}
Page last updated: 2011-3-12