static bool WaitForAnyKey(out string input, out bool isButton, out PositiveNegative direction) { float axisVal = 0; var axis = GetAxis(out axisVal); //Detect any axis (that has been defined within the function) if (!string.IsNullOrEmpty(axis)) //Has axis return anything? { input = axis; //Set output for "input" string isButton = false; //Set output for "isButton" bool if (axisVal > 0) //Was the axis found to be positive? { direction = PositiveNegative.Positive; //Set output for "direction" enum } else { direction = PositiveNegative.Negative; //Set output for "direction" enum } return(true); } //Loop through all possible buttons that can be pressed foreach (var key in Enum.GetNames(typeof(KeyCode)).ToList()) { if (key != "None") { var e = (KeyCode)Enum.Parse(typeof(KeyCode), key); //Convert button string to KeyCode enum if (UnityEngine.Input.GetKeyDown(e)) //Detect if it has been pressed { input = e.ToString(); //Set output for "input" string isButton = true; //Set output for "isButton" bool direction = PositiveNegative.Positive; //Set output for "direction" enum (default value since it is not relavant to this input) return(true); } } } //No input detected yet, return false will default values input = ""; isButton = false; direction = PositiveNegative.Negative; return(false); }
/// <summary> /// Runs operation to update a key binding /// </summary> /// <param name="button">The button you wish to update</param> /// <param name="type">The direction of the input that you want to update (Positive or Negative)</param> /// <param name="save">Whether or not you want to save the new key binding (recommended)</param> /// <param name="isAlternative">Whether or not this will be affecting the alternative key binding or just the normal key binding</param> /// <returns></returns> public static IEnumerator UpdateInput(string button, bool isAlternative = false, PositiveNegative type = PositiveNegative.Positive, bool save = false) { var b = Inputs.SingleOrDefault(x => x.Name == button); //Find the button with requested name if (b != null) //If no button exists, exit out of the function { string input = ""; bool isButton = false; PositiveNegative dir = 0; yield return(new WaitUntil(() => WaitForAnyKey(out input, out isButton, out dir))); //Wait until any input is detected (and assign values with details of said input) if (isButton) //Was a button and not an axis detected? { var code = (KeyCode)Enum.Parse(typeof(KeyCode), input); //Convert from string to KeyCode enum //Clear button axis if (isAlternative) { b.AlternativeAxis = ""; } else { b.Axis = ""; } switch (type) //Update button values { case PositiveNegative.Positive: if (isAlternative) { b.AlternativePositiveKey = code; } else { b.PositiveKey = code; } break; case PositiveNegative.Negative: if (isAlternative) { b.NegativeKey = code; } else { b.NegativeKey = code; } break; } } else { if (isAlternative) { //Clear button values b.AlternativePositiveKey = KeyCode.None; b.AlternativeNegativeKey = KeyCode.None; //Updated Unity axis b.AlternativeAxis = input; //Set Input direction b.AlternativeButtonDirection = dir; } else { //Clear button values b.PositiveKey = KeyCode.None; b.NegativeKey = KeyCode.None; //Updated Unity axis b.Axis = input; //Set Input direction b.ButtonDirection = dir; } } if (save) //Save if requests { DoSave(); } Debug.Log("Updated " + button + "'s " + type.ToString() + " key to " + input); } else { Debug.LogError("Button \"" + button + "\" could not be found"); } }