如何将 char 变量设置为 Input.GetKey 的 KeyCode?

How can I set a char variable to the KeyCode of Input.GetKey?

我正在尝试制作作弊码系统。我有一系列字符。我想将玩家输入的任何输入分配给该字符,然后将索引更改为下一个字符并重复该字符。最后,我想将所有字符组合成一个字符串,看看这是否是作弊码。如果是,那么玩家将获得通电或其他任何东西。

我基本上希望字符是我按下的任何按钮。有没有更好的方法来做到这一点,而不是像这样:

if (Input.GetKeyDown(KeyCode.A))
{
CodeAttempt[index] = 'a'
index++;
}
if (Input.GetKeyDown(KeyCode.C))
{
CodeAttempt[index] = 'b'
index++;
}
if (Input.GetKeyDown(KeyCode.C))
{
CodeAttempt[index] = 'c'
index++;
}

等等?

据我所知,Unity 没有内置的 KeyCodechar 映射。

我找到了一个包含 charKeyCode 字典的 Gist。只需交换键和值,您就可以开始了:https://gist.github.com/b-cancel/c516990b8b304d47188a7fa8be9a1ad9

我不想将代码放在答案中,因为它太长了,但我做了一个分支以防原始 Gist 失效:https://gist.github.com/starikcetin/62506e691ecd465159a235fb7acb44f0


编辑: 为什么不以 KeyCode 数组的形式保存作弊码?那么你就不必处理字符了。

您可以使用Input.anyKeyDown and Input.inputString(区分大小写):

private void Update()
{
    if( Input.anyKeyDown )
    {
        foreach( char c in Input.inputString )
            CodeAttempt[index++] = char.ToLowerInvariant( c );
    }
}