将键码Alpha转换为整数的更有效方法

时间:2017-05-21 20:19:06

标签: c# unity3d keyboard

在C#中,特别是在Unity脚本中,我可以使用以下行来了解在最后一帧中是否按下了一个键(此处为键盘上的数字1,非键盘)。

if (Input.GetKeyDown(KeyCode.Alpha1))
{
    MethodCall(1)
}

现在在我的游戏中,无论按哪个号码,我都希望调用完全相同的方法,但是使用该号码作为参数,我希望所有数字都能正常工作。所以我当然可以这样做:

if (Input.GetKeyDown(KeyCode.Alpha1))
{
    MethodCall(1)
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
    MethodCall(2)
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
    MethodCall(3)
}
// Etc

但这似乎是处理这个特定用例的非常低效的方法。

我已经考虑过简单地将按键转换为自己的char,然后转换为字符串,然后将其解析为int,但是我仍然需要做一个巨大的if来检查密钥是否至少是其中一个号。

那么,在代码长度和执行时间内,如果答案不一样,那么执行此特定任务的最有效方法是什么?

1 个答案:

答案 0 :(得分:1)

使用以Keycode为键的词典和函数(Action)作为值。使用Start函数中的相应参数添加函数。这将获得所有ifswitch声明。

Dictionary<KeyCode, System.Action> keyCodeDic = new Dictionary<KeyCode, System.Action>();

Alpha Keycode枚举从48开始,到57结束。

Alpha0 = 48,
Alpha1 = 49,
Alpha2 = 50,
Alpha3 = 51,
Alpha4 = 52,
Alpha5 = 53,
Alpha6 = 54,
Alpha7 = 55,
Alpha8 = 56,
Alpha9 = 57,

Start函数中,从48循环到57,然后将每个int值从循环转换为Keycode。在每个循环中将keycode添加到Dictionary。此外,还有一个临时变量,您将用作函数参数。此变量应从0开始,并在每次循环后递增。它应该从0开始,因为键盘上的Alpha键从0(Alpha0)开始:

int paramValue = 0;
for (int i = 48; i <= 57; i++)
{
    KeyCode tempKeyCode = (KeyCode)i;

    //Use temp variable to prevent it from being capture
    int temParam = paramValue;
    keyCodeDic.Add(tempKeyCode, () => MethodCall(temParam));
    paramValue++;
}

最后,在Update函数循环中通过Dictionary并检查是否按下了键,然后调用相应的函数,该函数是Dictionary中该键的值。

//Loop through the Dictionary and check if the Registered Keycode is pressed
foreach (KeyValuePair<KeyCode, System.Action> entry in keyCodeDic)
{
    //Check if the keycode is pressed
    if (Input.GetKeyDown(entry.Key))
    {
        //Check if the key pressed exist in the dictionary key
        if (keyCodeDic.ContainsKey(entry.Key))
        {
            //Debug.Log("Pressed" + entry.Key);

            //Call the function stored in the Dictionary's value
            keyCodeDic[entry.Key].Invoke();
        }
    }
}

以下是整个事情的样子:

Dictionary<KeyCode, System.Action> keyCodeDic = new Dictionary<KeyCode, System.Action>();

void Start()
{
    //Register Keycodes to to match each function to call
    const int alphaStart = 48;
    const int alphaEnd = 57;

    int paramValue = 0;
    for (int i = alphaStart; i <= alphaEnd; i++)
    {
        KeyCode tempKeyCode = (KeyCode)i;

        //Use temp variable to prevent it from being capture
        int temParam = paramValue;
        keyCodeDic.Add(tempKeyCode, () => MethodCall(temParam));
        paramValue++;
    }
}

void MethodCall(int keyNum)
{
    Debug.Log("Pressed: " + keyNum);
}


void Update()
{
    //Loop through the Dictionary and check if the Registered Keycode is pressed
    foreach (KeyValuePair<KeyCode, System.Action> entry in keyCodeDic)
    {
        //Check if the keycode is pressed
        if (Input.GetKeyDown(entry.Key))
        {
            //Check if the key pressed exist in the dictionary key
            if (keyCodeDic.ContainsKey(entry.Key))
            {
                //Debug.Log("Pressed" + entry.Key);

                //Call the function stored in the Dictionary's value
                keyCodeDic[entry.Key].Invoke();
            }
        }
    }
}