Last active
April 1, 2026 20:26
-
-
Save maoyeedy/0b372fc60cbe6a9f138841946652d30c to your computer and use it in GitHub Desktop.
[Unity] Toggle Play Mode with Gamepad Buttons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if UNITY_EDITOR && ENABLE_INPUT_SYSTEM | |
| using UnityEditor; | |
| using UnityEngine.InputSystem; | |
| namespace Project.Editor | |
| { | |
| [InitializeOnLoad] | |
| public static class GamepadPlayModeController | |
| { | |
| private static bool _wasPressingBoth; | |
| static GamepadPlayModeController() | |
| { | |
| // Reliable polling in both Edit and Play mode | |
| InputSystem.onAfterUpdate += CheckGamepadToggle; | |
| // Prevent instant reverts on Domain Reload | |
| EditorApplication.playModeStateChanged += OnPlayModeStateChanged; | |
| } | |
| private static void OnPlayModeStateChanged(PlayModeStateChange state) | |
| { | |
| // Force button release after state change to prevent double-toggling | |
| if (state == PlayModeStateChange.EnteredPlayMode || state == PlayModeStateChange.EnteredEditMode) | |
| { | |
| _wasPressingBoth = true; | |
| } | |
| } | |
| private static void CheckGamepadToggle() | |
| { | |
| var gamepad = Gamepad.current; | |
| if (gamepad == null) return; | |
| bool isPressingBoth = gamepad.startButton.isPressed && gamepad.selectButton.isPressed; | |
| // Trigger only on initial press | |
| if (isPressingBoth && !_wasPressingBoth) | |
| { | |
| EditorApplication.isPlaying = !EditorApplication.isPlaying; | |
| } | |
| _wasPressingBoth = isPressingBoth; | |
| } | |
| } | |
| } | |
| #endif |
Author
Author
Most of the time, I had to put gamepad aside so that I can press Ctrl+P or click the Play button.
Then I’d have to pick the gamepad up again to continue playtesting.
That is clearly user-unfriendly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unity Editor Play Mode Toggle
Enters/Exits play mode with Gamepad.
Default trigger is pressing both Start and Select.
Prerequisites:
Input System installed and enabled.