void Update() { if (PS4Input.PadIsConnected(playerId)) { // Set the gamepad to the start values for the player if (!hasSetupGamepad) { ToggleGamePad(true); } // Handle each part individually Touchpad(); Thumbsticks(); InputButtons(); DPadButtons(); TriggerShoulderButtons(); Lightbar(); Speaker(); // Options button is on its own, so we'll do it here if (Input.GetKey((KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + stickID + "Button7", true))) { gamePad.options.color = inputOn; // Reset the gyro orientation and lightbar to default PS4Input.PadResetOrientation(playerId); PS4Input.PadResetLightBar(playerId); lightbarColour = GetPlayerColor(PS4Input.GetUsersDetails(playerId).color); } else { gamePad.options.color = inputOff; } // Make the gyro rotate to match the physical controller gamePad.gyro.localEulerAngles = new Vector3(-PS4Input.PadGetLastOrientation(playerId).x, -PS4Input.PadGetLastOrientation(playerId).y, PS4Input.PadGetLastOrientation(playerId).z) * 100; // rebuild the username everyframe, in case it's changed due to PSN access gamePad.text.text = PS4Input.RefreshUsersDetails(playerId).userName + "\n(" + connectionType + ")"; } else if (hasSetupGamepad) { ToggleGamePad(false); } }
// Toggle the gamepad between connected and disconnected states void ToggleGamePad(bool active) { if (active) { // Set the lightbar colour to the start/default value lightbarColour = GetPlayerColor(PS4Input.GetUsersDetails(playerId).color); // Set 3D Text to whoever's using the pad loggedInUser = PS4Input.RefreshUsersDetails(playerId); gamePad.text.text = loggedInUser.userName + "\n(" + connectionType + ")"; // Reset and show the gyro gamePad.gyro.localRotation = Quaternion.identity; gamePad.gyro.gameObject.SetActive(true); hasSetupGamepad = true; } else { // Hide the touches touches[0].gameObject.SetActive(false); touches[1].gameObject.SetActive(false); // Set the lightbar to a default colour lightbarColour = Color.gray; gamePad.lightbar.color = lightbarColour; gamePad.light.color = Color.black; // Set the 3D Text to show the pad is disconnected gamePad.text.text = "Disconnected"; // Hide the gyro gamePad.gyro.gameObject.SetActive(false); hasSetupGamepad = false; } }