Skip to content

Instantly share code, notes, and snippets.

@maoyeedy
Last active April 1, 2026 20:26
Show Gist options
  • Select an option

  • Save maoyeedy/0b372fc60cbe6a9f138841946652d30c to your computer and use it in GitHub Desktop.

Select an option

Save maoyeedy/0b372fc60cbe6a9f138841946652d30c to your computer and use it in GitHub Desktop.
[Unity] Toggle Play Mode with Gamepad Buttons
#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
@maoyeedy

maoyeedy commented Apr 28, 2025

Copy link
Copy Markdown
Author

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.

@maoyeedy

Copy link
Copy Markdown
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