Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

converting alpha1 into int unity

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

void Start()
{
    //Register Keycodes 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();
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# create empty file if not exists 
Csharp :: .net disable show exception 
Csharp :: telerik mvc grid unbound column 
Csharp :: unity sprite blurry when far 
Csharp :: how to initialize array in c# 
Csharp :: convert excel to datatable without xml configuration 
Csharp :: notification platform not available c# 
Csharp :: c# interoperability with linux or bash script 
Csharp :: C# downloadstirng download old 
Csharp :: Unity search all chidren of parent object 
Csharp :: beard styles without mustache Intitle:work with me 
Csharp :: Delegate parameter no return 
Csharp :: c# directory entry invoke 
Csharp :: nullable 
Csharp :: C# list of unique values with group and counts 
Csharp :: c# compare 2 binary files 
Csharp :: get user by username c# 
Csharp :: c# download to string 
Csharp :: how to read reportview query string asp.net c# 
Csharp :: transformquestionmarks=OCR 
Csharp :: unity insert variable into string 
Csharp :: How to retrieve a dead letter msg dotnet 
Csharp :: resharper render pages folder asp.net core 
Csharp :: All and Any linq c# examlpe replace 
Csharp :: excute same code mvc 
Csharp :: ExpandoObject Make Objects Extensible 
Csharp :: Avoid auto-filling persian time picker 
Csharp :: c# get count from unknown list 
Csharp :: entity framework where date between 
Csharp :: C# today, yesterday, last week, last month 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =