在下面的代码中,不可能打印"FBleftcontrol",这也取决于按下的键的顺序和组合,有时其他行无法打印。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
bool LeftControl = false;
bool LeftShift = false;
bool LeftAlt = false;
// Update is called once per frame
void Update()
{
LeftControl = Input.GetKey(KeyCode.LeftControl);
LeftShift = Input.GetKey(KeyCode.LeftShift);
LeftAlt = Input.GetKey(KeyCode.LeftAlt);
if (LeftAlt || LeftShift) fb();
else fa();
}
private void fb()
{
print("fb");
if (LeftControl)
{
if (Input.GetKeyDown(KeyCode.Alpha1)) print("leftcontrol");
}
else
{
if (Input.GetKeyDown(KeyCode.Alpha1)) print("NOleftcontrol");
}
}
private void fa()
{
print("fa");
if (LeftControl)
{
if (Input.GetKeyDown(KeyCode.Alpha1)) print("leftcontrol");
}
else if(LeftShift)
{
if (Input.GetKeyDown(KeyCode.Alpha1)) print("leftshift");
}
}
}
我尝试过的事情:闩锁布尔值;使用getkey/getkeydown/getkeyup的所有可能组合;将其全部放在更新中,忘记调用函数
发布于 2019-03-05 04:35:13
Unity Editor偷走了你的键盘焦点,因为大多数修改键都绑定到它的GUI上。用KeyCode.A, KeyCode.S, KeyCode.C
代替Alt键、Shift键、Control键来尝试相同的代码,你的代码就会像预期的那样工作。
不过,这应该只在编辑器中发生,而不是在最终的构建中。
不幸的是,我不知道有什么解决方案可以在编辑器中避免这种情况:我只会在开发过程中使用不同的键绑定。
https://stackoverflow.com/questions/54973043
复制相似问题