public void CheckGamepadType(int padID) { if (padID == 0) { currentControlType = ControllerType.Keyboard; } if (playerInput == null) { playerInput = ReInput.players.GetPlayer(0); } if (playerInput.controllers.joystickCount == 0) { return; } Joystick gamepad = playerInput.controllers.Joysticks[padID - 1]; if (gamepad == null) { return; } for (int i = 0; i < aControllerGuids.Length; i++) { if (aControllerGuids[i] == null) { continue; } if (gamepad.hardwareTypeGuid != aControllerGuids[i].Guid) { continue; } currentGamepadType = (eGamepadButtonType)i; nativeGamepadType = currentGamepadType; return; } currentGamepadType = eGamepadButtonType.Generic; nativeGamepadType = currentGamepadType; }
// Initialization public override void Awake() { base.Awake(); playerInput = ReInput.players.GetPlayer(0); ReInput.ControllerConnectedEvent += SetPadXml; ReInput.ControllerConnectedEvent += GamepadConnectEvent; #if UNITY_EDITOR || UNITY_STANDALONE currentControlType = ControllerType.Keyboard; #else currentControlType = ControllerType.Joystick; #endif currentGamepadType = eGamepadButtonType.Xbox360; CheckCurrentGamepadForType(-1); }
// Sets the controller image based on control type, gamepad type and element id private void SetControllerImage() { if (iInputActionID == 0) { return; } ControllerType controlType = ControllerType.Keyboard; int controllerID = 0; // Check control type if (ControllerStatusManager.currentControlType == ControllerType.Keyboard || ControllerStatusManager.currentControlType == ControllerType.Mouse) { controlType = ControllerType.Keyboard; } else if (ControllerStatusManager.currentControlType == ControllerType.Joystick) { if (playerInput.controllers.joystickCount > 0) { controlType = ControllerType.Joystick; controllerID = ControllerStatusManager.iGamepadID; } } // Current controller Controller controller = ReInput.controllers.GetController(controlType, controllerID); // If there is no controller, set the controller to the keyboard if (controller == null) { controller = ReInput.controllers.GetController <Keyboard>(0); controlType = ControllerType.Keyboard; } // Map ID (Primary or Alt) int mapID = bAltMap ? iAltMapID : iPrimMapID; // Current map ActionElementMap map = null; // Mouse map ActionElementMap mouseMap = null; if (bFullAxisRange) { map = playerInput.controllers.maps.GetFirstElementMapWithAction(controller, iInputActionID, false); mouseMap = playerInput.controllers.maps.GetFirstElementMapWithAction(ControllerType.Mouse, iInputActionID, false); } else { ActionElementMap[] maps = playerInput.controllers.maps.GetFirstMapInCategory(controller.type, controllerID, mapID).GetElementMapsWithAction(iInputActionID); ActionElementMap[] mouseMaps = playerInput.controllers.maps.GetFirstMapInCategory(ControllerType.Mouse, controllerID, mapID).GetElementMapsWithAction(iInputActionID); for (int i = 0; i < maps.Length; i++) { if (maps[i].axisContribution == axisPole) { map = maps[i]; } } for (int j = 0; j < mouseMaps.Length; j++) { if (mouseMaps[j].axisContribution == axisPole) { mouseMap = mouseMaps[j]; } } } if (!bFullAxisRange) { if (axisPole == Pole.Positive) { map.axisRange = AxisRange.Positive; mouseMap.axisRange = AxisRange.Positive; } else { map.axisRange = AxisRange.Negative; mouseMap.axisRange = AxisRange.Negative; } } // If the gamepad is generic or gamepad type is set to generic, set text if (controlType == ControllerType.Joystick && ControllerStatusManager.currentGamepadType == eGamepadButtonType.Generic) { buttonSprite.gameObject.SetActive(false); buttonText.gameObject.SetActive(true); string prefix; if (!bIsAxis2D) { string axis = bAbbrevPrefixes ? "A" : "Axis "; string button = bAbbrevPrefixes ? "B" : "Button "; prefix = map.elementType == ControllerElementType.Axis ? axis : button; buttonText.text = prefix + map.elementIndex; } if (bIsAxis2D || map.elementType == ControllerElementType.Axis) { if (bIsAxis2D) { ActionElementMap primaryMap = playerInput.controllers.maps.GetFirstElementMapWithAction(controller, iPrimaryAxis2DActionID, false); ActionElementMap secondMap = playerInput.controllers.maps.GetFirstElementMapWithAction(controller, iSecondaryAxis2DActionID, false); buttonText.text = "Axis " + primaryMap.elementIndex + " / " + "Axis " + secondMap.elementIndex; } else { if (map.axisRange == AxisRange.Positive) { buttonText.text += "+"; } else if (map.axisRange == AxisRange.Negative) { buttonText.text += "-"; } } } } else // Otherwise, set image { buttonSprite.gameObject.SetActive(true); buttonText.gameObject.SetActive(false); if (controlType == ControllerType.Keyboard) { if (map.elementIdentifierName == "None" || map.elementIdentifierName == string.Empty) { buttonSprite.sprite = inputImageData.GetImage(ControllerType.Mouse, mouseMap.elementType, mouseMap.elementIdentifierId, mouseMap.axisRange, axisPole); } else { buttonSprite.sprite = inputImageData.GetImage(ControllerType.Keyboard, ControllerElementType.Button, (int)map.keyCode, map.axisRange, axisPole); } } else { if (bIsAxis2D) { buttonSprite.sprite = inputImageData.GetImageAxis2D(ControllerType.Joystick, iInputAxis2DImageID); } else { buttonSprite.sprite = inputImageData.GetImage(ControllerType.Joystick, map.elementType, map.elementIdentifierId, map.axisRange, map.axisContribution); } } } currentControlType = ControllerStatusManager.currentControlType; currentGamepadType = ControllerStatusManager.currentGamepadType; }