Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Put this script in a Editor/ folder somewhere...
using System.Collections;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
using System.IO;
using System.Diagnostics;
[InitializeOnLoad]
public static class ShortcutFixForUnity2019
{
static bool FirstCallThisSession
{
get
{
// returns true once then false other times...
// reset each time you relaunch unity
string tempFolder = Application.dataPath.Replace("/Assets", "/Temp");
string tempFile = tempFolder + "/fixShortcut";
bool customTempFileExists = File.Exists(tempFile);
if (!customTempFileExists)
File.Create(tempFile);
return !customTempFileExists;
}
}
static ShortcutFixForUnity2019()
{
if (FirstCallThisSession)
{
// If this is the first call to InitializeOnLoad (unity was just openned)
// Then we need to wait a bit before calling the FixShortcut function
EditorApplication.update += Update;
routine = FixShortcutAfterDelay();
}
else
FixShortcut();
}
static void FixShortcut()
{
Type ShortcutManagerWindowType = null;
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
// Find UnityEditor Assembly
if (asm.FullName.StartsWith("UnityEditor,"))
ShortcutManagerWindowType = asm.GetType("UnityEditor.ShortcutManagement.ShortcutManagerWindow");
}
if (ShortcutManagerWindowType == null)
{
UnityEngine.Debug.LogWarning("Cant fix shortcut... type 'UnityEditor.ShortcutManagement.ShortcutManagerWindow' not found");
return;
}
var GetWindowDontShowMethod = typeof(EditorWindow).GetMethod("GetWindowDontShow", BindingFlags.Static | BindingFlags.NonPublic);
GetWindowDontShowMethod.MakeGenericMethod(ShortcutManagerWindowType).Invoke(null, null);
}
// Coroutine stuff ==========================================
static IEnumerator routine;
static void Update()
{
if (!routine.MoveNext())
{
routine = null;
EditorApplication.update -= Update;
}
}
static IEnumerator FixShortcutAfterDelay()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int secondsToWait = 2;
UnityEngine.Debug.Log("Wait " + secondsToWait + " seconds before fixing shortcuts");
while (stopwatch.ElapsedMilliseconds < secondsToWait * 1000)
yield return null;
UnityEngine.Debug.Log("Fix shortcut");
FixShortcut();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment