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 鍵的時候顯示
}
}
}