对于我的游戏,我需要检测逗号、句号和减号键的按下。尽管检测到其他键在工作,但它们都不起作用。我怀疑这与我有一个德语键盘有关,但不知道如何修复它。帮助将不胜感激!
Input.GetKeyDown(KeyCode.Comma)
Input.GetKeyDown(KeyCode.Minus)
Input.GetKeyDown(KeyCode.Period)
我怀疑这与我有德语键盘有关
要简单地调试它,您可以尝试以下检查按键
private void Update() {
foreach(KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))){
if(Input.GetKey(vKey)){
Debug.Log($"Pressed {vKey.ToString()}");
}
}
}
上面的代码将调试当前按下的键,所以如果你按下逗号并输出逗号,那么你可能做错了什么。
您可能会错误地调用它们。例子:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("space"))
{
print("space key was pressed");
}
}
}
https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
我一直在使用具有德语布局的键盘,但以下效果很好(不过,我认为这与您所做的相同):
void Update()
{
if (Input.GetKeyDown(KeyCode.Comma)) {
Debug.Log("Comma was pressed");
}
if (Input.GetKeyDown(KeyCode.Minus))
{
Debug.Log("Minus was pressed");
}
if (Input.GetKeyDown(KeyCode.Period))
{
Debug.Log("Period was pressed");
}
}
我也一直在使用具有不同语言布局的相同德语键盘(因此,逗号和其他的位置发生了变化),但它也能正常工作。我认为这不是因为键盘布局。
还有另一种编写输入检测的方法,如下所示:
if (Input.GetKeyDown(",")) {
Debug.Log("Comma was pressed");
}
如果您使用 OnGUI() 函数,这也可用...
private void OnGUI()
{
if (Event.current.Equals(Event.KeyboardEvent(",")))
{
Debug.Log("Comma was pressed");
}
}