private void GetCorrectPlayer(string p, KeybindEnum command, KeyCode keyValue) { var temp = playerKeybinds.ToList().FindIndex(playerIndex => string.Compare(playerIndex.GetPlayerIdentity(), p) == 0); if (temp != -1) { playerKeybinds[temp].SetKey(command, keyValue); } }
/* * Will change the KeyCode in the array and dictionary. */ public void SetKey(KeybindEnum command, KeyCode toSet, bool reset = false) { var commandPosition = playerInput.ToList().FindIndex(k => k.command == command); if (keyBindingsDictionary.ContainsKey(command) == false || commandPosition <= -1) { Debug.LogError($"Error when trying to set the key of command {command}."); return; } if (!reset) { CheckOverlappingKeybinds(toSet); } playerInput[commandPosition].keyCode = toSet; keyBindingsDictionary[command] = toSet; }
/* ----------------------------------------------- * PRIVATE METHODS * -----------------------------------------------*/ /* * Listener method to change keybindings. */ private IEnumerator ListenForKeyInput(KeybindEnum command, string p) { while (!Input.GetKeyDown(KeyCode.Escape)) { foreach (KeyCode keyValue in System.Enum.GetValues(typeof(KeyCode))) { if (Input.GetKeyDown(keyValue)) { if (IgnoreInput(keyValue)) { continue; } GetCorrectPlayer(p, command, keyValue); SetGameObjectTexts(false); listeningForInput = null; yield break; } } yield return(new WaitForSecondsRealtime(0)); } SetGameObjectTexts(false); listeningForInput = null; }
/* * Method to help organize the UI. */ private int FindCorrectCommand(KeybindEnum toFind, InputKeys[] searchArea) => searchArea.ToList().FindIndex(a => a.command == toFind);
/* * Use this method to set the value of keys as text. eg in options menu */ public string GetKeyAsString(KeybindEnum command) => keyBindingsDictionary[command].ToString();
/* ----------------------------------------------- * PUBLIC METHODS * -----------------------------------------------*/ /* * Use this method with Input.GetKey(...); */ public KeyCode GetKeyCode(KeybindEnum command) => keyBindingsDictionary[command];