Skip to content

Instantly share code, notes, and snippets.

@rheirman
Created December 30, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheirman/8cc1d578ffe7f4cea22b85651ba9e0b3 to your computer and use it in GitHub Desktop.
Save rheirman/8cc1d578ffe7f4cea22b85651ba9e0b3 to your computer and use it in GitHub Desktop.
Clear Unity editor shortcuts
using UnityEngine;
using UnityEditor;
using UnityEditor.ShortcutManagement;
using System.Linq;
using System;
[InitializeOnLoad]
public class SwitchShortcutsProfileOnPlay
{
private const string PlayingProfileId = "Playing";//Make you don't already have a profile named like this
private static string _previousProfileId;
private static bool _switched;
static SwitchShortcutsProfileOnPlay()
{
EditorApplication.playModeStateChanged += DetectPlayModeState;
}
private static void SetActiveProfile(string profileId)
{
Debug.Log($"Activating Shortcut profile \"{profileId}\"");
ShortcutManager.instance.activeProfileId = profileId;
}
private static void DetectPlayModeState(PlayModeStateChange state)
{
switch (state)
{
case PlayModeStateChange.EnteredPlayMode:
OnEnteredPlayMode();
break;
case PlayModeStateChange.ExitingPlayMode:
OnExitingPlayMode();
break;
}
}
private static void OnExitingPlayMode()
{
if (!_switched)
return;
SetActiveProfile(_previousProfileId);
_switched = false;
}
private static void CreateEmptyProfile()
{
try
{
ShortcutManager.instance.CreateProfile(PlayingProfileId);
ShortcutManager.instance.activeProfileId = PlayingProfileId;
foreach (var pid in ShortcutManager.instance.GetAvailableShortcutIds())
ShortcutManager.instance.RebindShortcut(pid, ShortcutBinding.empty);
ShortcutManager.instance.activeProfileId = ShortcutManager.defaultProfileId;
}
catch (Exception)
{
Debug.LogWarning("Couldn't create profile");
}
}
private static void OnEnteredPlayMode()
{
_previousProfileId = ShortcutManager.instance.activeProfileId;
var allProfiles = ShortcutManager.instance.GetAvailableProfileIds().ToList();
if (!allProfiles.Contains(PlayingProfileId)) {
CreateEmptyProfile();
}
if (_previousProfileId.Equals(PlayingProfileId))
return; // Same as active
_switched = true;
SetActiveProfile(PlayingProfileId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment