Input.GetKey 获取键

static function GetKey (name : string) : bool

Description描述

Returns true while the user holds down the key identified by name. Think auto fire.

当通过名称指定的按键被用户按住时返回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.GetKey("up"))
			print("up arrow key is held down");

		if (Input.GetKey("down"))
			print("down arrow key is held down");

	}
}
function Update () {
	if (Input.GetKey ("up"))
		print ("up arrow key is held down");

	if (Input.GetKey ("down"))
		print ("down arrow key is held down");
}

• static function GetKey (key : KeyCode) : bool

Description描述

Returns true while the user holds down the key identified by the key KeyCode enum parameter.

当通过按键枚举参数指定的按键被用户按住时返回true。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Update() {
		if (Input.GetKey(KeyCode.UpArrow))
			print("up arrow key is held down");

		if (Input.GetKey(KeyCode.DownArrow))
			print("down arrow key is held down");

	}
}
function Update () {
	if (Input.GetKey (KeyCode.UpArrow))
		print ("up arrow key is held down");

	if (Input.GetKey (KeyCode.DownArrow))
		print ("down arrow key is held down");
}
最后修改:2011年3月12日 Saturday 19:31

本脚本参考基于Unity 3.4.1f5

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