如何将字符变量设置为 Input.GetKey 的 KeyCode? [英] How can I set a char variable to the KeyCode of Input.GetKey?

查看:19
本文介绍了如何将字符变量设置为 Input.GetKey 的 KeyCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a cheat code system. I had an array of chars. I want to assign whatever input the player puts in to that char, and then ill change the index to the next char and repeat with that. At the end I want to combine all the chars together to a string and see if that's a cheat code. If it is then the player will get a powerup or whatever.

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

I basically want the char to be whatever button I press. Is there any better way to do it that's not like this:

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++;
}

等等?

推荐答案

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

You can use Input.anyKeyDown and Input.inputString (case-sensitive):

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

这篇关于如何将字符变量设置为 Input.GetKey 的 KeyCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆