void Update() { GamepadState state = new GamepadState(); if (GamepadWatcher.Current.IsPresent) { state.AButtonPressed = Input.GetButtonDown(UnityInputAxis.XboxController_AButton); state.BButtonPressed = Input.GetButtonDown(UnityInputAxis.XboxController_BButton); state.XButtonPressed = Input.GetButtonDown(UnityInputAxis.XboxController_XButton); state.YButtonPressed = Input.GetButtonDown(UnityInputAxis.XboxController_YButton); state.LeftBumperPressed = Input.GetButtonDown(UnityInputAxis.XboxController_LeftBumper); state.RightBumperPressed = Input.GetButtonDown(UnityInputAxis.XboxController_RightBumper); state.BackButtonPressed = Input.GetButtonDown(UnityInputAxis.XboxController_BackButton); state.StartButtonPressed = Input.GetButtonDown(UnityInputAxis.XboxController_StartButton); state.LeftStickClicked = Input.GetButtonDown(UnityInputAxis.XboxController_LeftStickButton); state.RightStickClicked = Input.GetButtonDown(UnityInputAxis.XboxController_RightStickButton); state.RightStickX = Input.GetAxis(UnityInputAxis.XboxController_RightStickX); state.RightStickY = Input.GetAxis(UnityInputAxis.XboxController_RightStickY); state.LeftStickX = Input.GetAxis(UnityInputAxis.XboxController_LeftStickX); state.LeftStickY = Input.GetAxis(UnityInputAxis.XboxController_LeftStickY); state.DPadX = Input.GetAxis(UnityInputAxis.XboxController_DPadX); state.DPadY = Input.GetAxis(UnityInputAxis.XboxController_DPadY); float trigger = Input.GetAxis(UnityInputAxis.XboxController_Trigger); if (trigger != 0) { if (trigger < 0) { state.LeftTrigger = trigger; } else { state.RightTrigger = trigger; } } bool hasChanges; string gamepadState = state.GetTraceState(out hasChanges); if (hasChanges) { Debug.Log(gamepadState); } } }
public static string GetTraceState(this GamepadState state, out bool hasNonDefaultValue) { StringBuilder sb = new StringBuilder(); hasNonDefaultValue = false; sb.Append("Gamepad:"); if (state.AButtonPressed) { sb.Append("A-Pressed"); hasNonDefaultValue = true; } if (state.BButtonPressed) { sb.Append("B-Pressed"); hasNonDefaultValue = true; } if (state.XButtonPressed) { sb.Append("X-Pressed"); hasNonDefaultValue = true; } if (state.YButtonPressed) { sb.Append("Y-Pressed"); hasNonDefaultValue = true; } if (state.BackButtonPressed) { sb.Append("Back-Pressed"); hasNonDefaultValue = true; } if (state.StartButtonPressed) { sb.Append("Start-Pressed"); hasNonDefaultValue = true; } if (state.LeftBumperPressed) { sb.Append("LB:Pressed"); hasNonDefaultValue = true; } if (state.RightBumperPressed) { sb.Append("RB:Pressed"); hasNonDefaultValue = true; } if (state.LeftStickClicked) { sb.Append("LS:Pressed"); hasNonDefaultValue = true; } if (state.RightStickClicked) { sb.Append("RS:Pressed"); hasNonDefaultValue = true; } if (state.LeftStickX != 0f || state.LeftStickY != 0f) { sb.AppendFormat("LS ({0},{1})", state.LeftStickX, state.LeftStickY); hasNonDefaultValue = true; } if (state.RightStickX != 0f || state.RightStickY != 0f) { sb.AppendFormat("RS ({0},{1})", state.RightStickX, state.RightStickY); hasNonDefaultValue = true; } if (state.DPadX != 0f || state.DPadY != 0f) { sb.AppendFormat("DPad ({0},{1})", state.DPadX, state.DPadY); hasNonDefaultValue = true; } if (state.LeftTrigger != 0f || state.RightTrigger != 0f) { sb.AppendFormat("Trigger ({0},{1})", state.LeftTrigger, state.RightTrigger); hasNonDefaultValue = true; } if (hasNonDefaultValue) { return(sb.ToString()); } else { return(string.Empty); } }