Version: 2022.3

Input.GetKeyDown

切换到手册
public static bool GetKeyDown (string name);

描述

在用户开始按下 name 标识的键的帧期间返回 true。

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.

有关键标识符的列表,请参阅传统游戏输入。 处理输入时,建议改用 Input.GetAxisInput.GetButton, 因为这允许终端用户对键进行配置。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Update() { if (Input.GetKeyDown("space")) { Debug.Log("space key was pressed"); } } }

public static bool GetKeyDown (KeyCode key);

描述

在用户开始按下 key KeyCode 枚举参数标识的键的帧期间返回 true。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("space key was pressed"); } } }