/// <summary> /// Get the KeyCode that corresponds to a specific gamepad number and button /// </summary> /// <param name="slotIndex">0-index based (starts from 0)</param> /// <param name="gamepadButtonNumber">0-index based (starts from 0)</param> public static KeyCode GetGamepadKeyCode(int slotIndex, int gamepadButtonNumber) { const UnityGamepadKeyCode FirstButton = UnityGamepadKeyCode.Joystick1Button0; const int ButtonsPerGamepad = UnityGamepadKeyCode.Joystick2Button0 - UnityGamepadKeyCode.Joystick1Button0; if (slotIndex < 0 || slotIndex >= 16 || gamepadButtonNumber < 0 || gamepadButtonNumber >= 20) { return(KeyCode.None); } return((KeyCode)FirstButton + slotIndex * ButtonsPerGamepad + gamepadButtonNumber); }
//public float virtualAxisValue; public bool ButtonCheck(ButtonAction bAction, InputDeviceSlot slot) { //keyboard key checks if (inputType == InputDeviceType.Keyboard) { if (slot == InputDeviceSlot.any || slot == InputDeviceSlot.keyboard || slot == InputDeviceSlot.keyboardAndMouse) { if (bAction == ButtonAction.HELD) { return(Input.GetKey(keyboardKeyCode)); } if (bAction == ButtonAction.DOWN) { return(Input.GetKeyDown(keyboardKeyCode)); } if (bAction == ButtonAction.UP) { return(Input.GetKeyUp(keyboardKeyCode)); } } return(false); } //gamepad button checks if (inputType == InputDeviceType.GamepadButton || inputType == InputDeviceType.GamepadAxis) { if (slot == InputDeviceSlot.keyboard || slot == InputDeviceSlot.mouse || slot == InputDeviceSlot.keyboardAndMouse) { return(false); } //if checking any slot, call this function for each possible slot if (slot == InputDeviceSlot.any) { return(ButtonCheck(bAction, InputDeviceSlot.gamepad1) || ButtonCheck(bAction, InputDeviceSlot.gamepad2) || ButtonCheck(bAction, InputDeviceSlot.gamepad3) || ButtonCheck(bAction, InputDeviceSlot.gamepad4) || ButtonCheck(bAction, InputDeviceSlot.gamepad5) || ButtonCheck(bAction, InputDeviceSlot.gamepad6) || ButtonCheck(bAction, InputDeviceSlot.gamepad7) || ButtonCheck(bAction, InputDeviceSlot.gamepad7) || ButtonCheck(bAction, InputDeviceSlot.gamepad9) || ButtonCheck(bAction, InputDeviceSlot.gamepad10) || ButtonCheck(bAction, InputDeviceSlot.gamepad11) || ButtonCheck(bAction, InputDeviceSlot.gamepad12) || ButtonCheck(bAction, InputDeviceSlot.gamepad13) || ButtonCheck(bAction, InputDeviceSlot.gamepad14) || ButtonCheck(bAction, InputDeviceSlot.gamepad15) || ButtonCheck(bAction, InputDeviceSlot.gamepad16)); } int slotIndex = ((int)slot) - 1; //don't check slots without a connected gamepad if (Sinput.gamepads.Length <= slotIndex) { return(false); } //make sure the gamepad in this slot is one this input is allowed to check (eg don't check PS4 pad bindings for an XBOX pad) bool allowInputFromThisPad = false; for (int i = 0; i < allowedSlots.Length; i++) { if (slotIndex == allowedSlots[i]) { allowInputFromThisPad = true; } } if (!allowInputFromThisPad) { return(false); } //gamepad button check if (inputType == InputDeviceType.GamepadButton) { //get the keycode for the gamepad's slot/button string buttonString = string.Format("Joystick{0}Button{1}", (slotIndex + 1), gamepadButtonNumber); if (string.IsNullOrEmpty(buttonString)) { return(false); } UnityGamepadKeyCode keyCode = (UnityGamepadKeyCode)System.Enum.Parse(typeof(UnityGamepadKeyCode), buttonString); //button check now if (bAction == ButtonAction.HELD) { return(Input.GetKey((KeyCode)(int)keyCode)); } if (bAction == ButtonAction.DOWN) { return(Input.GetKeyDown((KeyCode)(int)keyCode)); } if (bAction == ButtonAction.UP) { return(Input.GetKeyUp((KeyCode)(int)keyCode)); } } //gamepad axis as a button check if (inputType == InputDeviceType.GamepadAxis) { if (bAction == axisButtonState[slotIndex + 1]) { return(true); } if (bAction == ButtonAction.HELD && axisButtonState[slotIndex + 1] == ButtonAction.DOWN) { return(true); } return(false); } return(false); } //virtual device input checks if (inputType == InputDeviceType.Virtual) { if (slot == InputDeviceSlot.any || slot == InputDeviceSlot.virtual1) { virtualInputState = VirtualInputs.GetVirtualButton(virtualInputID); if (bAction == virtualInputState) { return(true); } if (bAction == ButtonAction.HELD && virtualInputState == ButtonAction.DOWN) { return(true); } } } //mouseaxis button checks (these don't happen) if (inputType == InputDeviceType.Mouse) { if (slot != InputDeviceSlot.any && slot != InputDeviceSlot.mouse && slot != InputDeviceSlot.keyboardAndMouse) { return(false); } KeyCode mouseKeyCode = KeyCode.None; if (mouseInputType == MouseInputType.Mouse0) { mouseKeyCode = KeyCode.Mouse0; } if (mouseInputType == MouseInputType.Mouse1) { mouseKeyCode = KeyCode.Mouse1; } if (mouseInputType == MouseInputType.Mouse2) { mouseKeyCode = KeyCode.Mouse2; } if (mouseInputType == MouseInputType.Mouse3) { mouseKeyCode = KeyCode.Mouse3; } if (mouseInputType == MouseInputType.Mouse4) { mouseKeyCode = KeyCode.Mouse4; } if (mouseInputType == MouseInputType.Mouse5) { mouseKeyCode = KeyCode.Mouse5; } if (mouseInputType == MouseInputType.Mouse6) { mouseKeyCode = KeyCode.Mouse6; } if (mouseKeyCode != KeyCode.None) { //clicky mouse input if (bAction == ButtonAction.HELD) { return(Input.GetKey(mouseKeyCode)); } if (bAction == ButtonAction.DOWN) { return(Input.GetKeyDown(mouseKeyCode)); } if (bAction == ButtonAction.UP) { return(Input.GetKeyUp(mouseKeyCode)); } } else { //mouse axis as button input if (bAction == axisButtonState[0]) { return(true); } if (bAction == ButtonAction.HELD && axisButtonState[0] == ButtonAction.DOWN) { return(true); } } return(false); } return(false); }
public float AxisCheck(InputDeviceSlot slot) { //keyboard checks if (inputType == InputDeviceType.Keyboard) { if (slot == InputDeviceSlot.any || slot == InputDeviceSlot.keyboard || slot == InputDeviceSlot.keyboardAndMouse) { if (Input.GetKey(keyboardKeyCode)) { return(1f); } } return(0f); } //gamepad button and axis checks if (inputType == InputDeviceType.GamepadButton || inputType == InputDeviceType.GamepadAxis) { if (slot == InputDeviceSlot.keyboard || slot == InputDeviceSlot.mouse || slot == InputDeviceSlot.keyboardAndMouse) { return(0f); } //if checking any slot, call this function for each possible slot if (slot == InputDeviceSlot.any) { float greatestV = 0f; for (int i = 1; i <= Sinput.gamepads.Length; i++) { greatestV = Mathf.Max(greatestV, Mathf.Abs(AxisCheck((InputDeviceSlot)i))); } return(greatestV); } int slotIndex = ((int)slot) - 1; //don't check slots without a connected gamepad if (Sinput.gamepads.Length <= slotIndex) { return(0f); } //make sure the gamepad in this slot is one this input is allowed to check (eg don't check PS4 pad bindings for an XBOX pad) bool allowInputFromThisPad = false; for (int i = 0; i < allowedSlots.Length; i++) { if (slotIndex == allowedSlots[i]) { allowInputFromThisPad = true; } } if (!allowInputFromThisPad) { return(0f); } //button as axis checks if (inputType == InputDeviceType.GamepadButton) { string buttonString = string.Format("Joystick{0}Button{1}", (slotIndex + 1), gamepadButtonNumber); if (string.IsNullOrEmpty(buttonString)) { return(0f); } UnityGamepadKeyCode keyCode = (UnityGamepadKeyCode)System.Enum.Parse(typeof(UnityGamepadKeyCode), buttonString); //button check now if (Input.GetKey((KeyCode)(int)keyCode)) { return(1f); } } //gamepad axis check if (inputType == InputDeviceType.GamepadAxis) { string axisString = string.Format("J_{0}_{1}", (slotIndex + 1), gamepadAxisNumber); float axisValue = Input.GetAxisRaw(axisString); if (invertAxis) { axisValue *= -1f; } if (rescaleAxis) { //some gamepad axis are -1 to 1 or something when you want them as 0 to 1, EG; triggers on XBONE pad on OSX axisValue = Mathf.InverseLerp(rescaleAxisMin, rescaleAxisMax, axisValue); } if (clampAxis) { axisValue = Mathf.Clamp01(axisValue); } //we return every axis' default value unless we measure a change first //this prevents weird snapping and false button presses if the pad is reporting a weird value to start with if (useDefaultAxisValue) { if (measuredAxisValue != -54.321f) { if (axisValue != measuredAxisValue) { useDefaultAxisValue = false; } } else { measuredAxisValue = axisValue; } if (useDefaultAxisValue) { axisValue = defaultAxisValue; } } return(axisValue); } return(0f); } //virtual device axis input checks if (inputType == InputDeviceType.Virtual) { if (slot == InputDeviceSlot.any || slot == InputDeviceSlot.virtual1) { return(VirtualInputs.GetVirtualAxis(virtualInputID)); } //return virtualAxisValue; } //mouseaxis button checks (these don't happen) if (inputType == InputDeviceType.Mouse) { if (slot != InputDeviceSlot.any && slot != InputDeviceSlot.mouse && slot != InputDeviceSlot.keyboardAndMouse) { return(0f); } switch (mouseInputType) { case MouseInputType.MouseHorizontal: return(Input.GetAxisRaw("Mouse Horizontal") * Sinput.mouseSensitivity); case MouseInputType.MouseMoveLeft: return(Mathf.Min(Input.GetAxisRaw("Mouse Horizontal") * Sinput.mouseSensitivity, 0f) * -1f); case MouseInputType.MouseMoveRight: return(Mathf.Max(Input.GetAxisRaw("Mouse Horizontal") * Sinput.mouseSensitivity, 0f)); case MouseInputType.MouseMoveUp: return(Mathf.Max(Input.GetAxisRaw("Mouse Vertical") * Sinput.mouseSensitivity, 0f)); case MouseInputType.MouseMoveDown: return(Mathf.Min(Input.GetAxisRaw("Mouse Vertical") * Sinput.mouseSensitivity, 0f) * -1f); case MouseInputType.MouseVertical: return(Input.GetAxisRaw("Mouse Vertical") * Sinput.mouseSensitivity); case MouseInputType.MouseScroll: return(Input.GetAxisRaw("Mouse Scroll")); case MouseInputType.MouseScrollUp: return(Mathf.Max(Input.GetAxisRaw("Mouse Scroll"), 0f)); case MouseInputType.MouseScrollDown: return(Mathf.Min(Input.GetAxisRaw("Mouse Scroll"), 0f) * -1f); case MouseInputType.MousePositionX: return(Input.mousePosition.x); case MouseInputType.MousePositionY: return(Input.mousePosition.y); default: //it's a click type mouse input if (Input.GetKey((KeyCode)(System.Enum.Parse(typeof(KeyCode), mouseInputType.ToString())))) { return(1f); } break; } //return Input.GetAxisRaw(mouseAxis); } return(0f); }