Unity的C#编程_2_输入信息/键盘输入
- 2020 年 7 月 11 日
- AI
- C/C++/C#
- 创建一个 C# Script,添加到 Main Camera 上面,并打开
- 所有的用户输入,需要在 Update() 函数方法中进行
- 假设我们监控空格键,按下的时候显示一个信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
// 使用 Input 类监控输入
// 检测按下按键 GetKeyDown
// 按下的空格键 KeyCode.Space
{
Debug.Log("Space Key");
// 在调试窗口显示信息 Space Key
}
if (Input.GetKey(KeyCode.E))
// 按住 E 这个键的时候
{
Debug.Log("Holding E");
// 在调试窗口显示信息 Holding E
}
if (Input.GetKeyUp(KeyCode.F))
{
Debug.Log("F"); // 放开 F 键的时候显示
}
}
}