Last active
March 6, 2022 22:22
-
-
Save thebeardphantom/677597d45eea00186fa0b7f997e5e557 to your computer and use it in GitHub Desktop.
Automatically switches editor shortcut profile when playing. Based on https://gist.github.com/RodGreen/8d5f5452c459755d9bf6bad1015ef194
This file contains 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
using System; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.ShortcutManagement; | |
using UnityEngine; | |
[InitializeOnLoad] | |
public class PlayModeShortcutManagerProfileUtility | |
{ | |
#region Types | |
private class PlayModeShortcutProfileScope : IDisposable | |
{ | |
#region Fields | |
private readonly string _lastEditModeProfile; | |
private bool _disposed; | |
#endregion | |
#region Constructors | |
public PlayModeShortcutProfileScope() | |
{ | |
var shortcutManager = ShortcutManager.instance; | |
_lastEditModeProfile = shortcutManager.activeProfileId; | |
if (HasPlayModeProfile) | |
{ | |
shortcutManager.activeProfileId = PLAY_MODE_PROFILE_ID; | |
} | |
else | |
{ | |
Debug.LogError($"Missing Shortcut Manager profile '{PLAY_MODE_PROFILE_ID}'. Will recreate on next recompile."); | |
} | |
} | |
#endregion | |
#region Methods | |
/// <inheritdoc /> | |
public void Dispose() | |
{ | |
if (_disposed) | |
{ | |
throw new InvalidOperationException("Object is already disposed."); | |
} | |
_disposed = true; | |
ShortcutManager.instance.activeProfileId = _lastEditModeProfile; | |
} | |
#endregion | |
} | |
#endregion | |
#region Fields | |
private const string PLAY_MODE_PROFILE_ID = "Play Mode"; | |
private static readonly string[] _ignoredIdPrefixes = | |
{ | |
"3D Viewport/", | |
"Window/Fullscreen Game View", | |
"Main Menu/Edit/Step", | |
"Main Menu/Edit/Pause" | |
}; | |
private static PlayModeShortcutProfileScope _lastScope; | |
#endregion | |
#region Properties | |
private static bool HasPlayModeProfile | |
{ | |
get | |
{ | |
var shortcutManager = ShortcutManager.instance; | |
var allProfiles = shortcutManager.GetAvailableProfileIds(); | |
return allProfiles.Contains(PLAY_MODE_PROFILE_ID); | |
} | |
} | |
#endregion | |
#region Constructors | |
static PlayModeShortcutManagerProfileUtility() | |
{ | |
EditorApplication.playModeStateChanged += OnPlayModeStateChanged; | |
EditorApplication.delayCall += CreatePlayModeProfile; | |
} | |
#endregion | |
#region Methods | |
private static void CreatePlayModeProfile() | |
{ | |
if (HasPlayModeProfile) | |
{ | |
return; | |
} | |
var shortcutManager = ShortcutManager.instance; | |
shortcutManager.CreateProfile(PLAY_MODE_PROFILE_ID); | |
// Ensure playmode profile is (almost) completely empty | |
using (new PlayModeShortcutProfileScope()) | |
{ | |
foreach (var shortcutId in shortcutManager.GetAvailableShortcutIds()) | |
{ | |
var shouldIgnore = false; | |
foreach (var prefix in _ignoredIdPrefixes) | |
{ | |
if (shortcutId.StartsWith(prefix)) | |
{ | |
shouldIgnore = true; | |
break; | |
} | |
} | |
if (shouldIgnore) | |
{ | |
continue; | |
} | |
if (shortcutManager.IsShortcutOverridden(shortcutId)) | |
{ | |
shortcutManager.ClearShortcutOverride(shortcutId); | |
} | |
shortcutManager.RebindShortcut(shortcutId, ShortcutBinding.empty); | |
} | |
} | |
} | |
private static void OnPlayModeStateChanged(PlayModeStateChange state) | |
{ | |
switch (state) | |
{ | |
case PlayModeStateChange.EnteredPlayMode: | |
{ | |
SetUsePlayModeProfile(true); | |
break; | |
} | |
case PlayModeStateChange.EnteredEditMode: | |
{ | |
SetUsePlayModeProfile(false); | |
break; | |
} | |
} | |
} | |
private static void SetUsePlayModeProfile(bool use) | |
{ | |
_lastScope?.Dispose(); | |
_lastScope = default; | |
if (use) | |
{ | |
_lastScope = new PlayModeShortcutProfileScope(); | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment