Skip to content

Instantly share code, notes, and snippets.

@dimmduh
Created May 31, 2019 15:10
Show Gist options
  • Save dimmduh/26b51f0628e3ee2649376269e95b2b9d to your computer and use it in GitHub Desktop.
Save dimmduh/26b51f0628e3ee2649376269e95b2b9d to your computer and use it in GitHub Desktop.
Unity reset layout by hotkey (default Ctrl + Shift + R). Put script to any Editor folder.
using System;
using System.IO;
using System.Reflection;
using UnityEditor;
public static class LayoutHotKey
{
const string layoutName = "Temp/autosave.wlt";
// % (ctrl), # (shift), & (alt)
//to change check docs https://docs.unity3d.com/ScriptReference/MenuItem.html
[MenuItem("Window/Layouts/Reset Layout %#r")]
static void ResetLayout()
{
Save();
Load();
}
static void Save() {
LayoutUtility.SaveLayout(layoutName);
}
static void Load() {
LayoutUtility.LoadLayout(layoutName);
}
}
public static class LayoutUtility {
private enum MethodType {Save, Load};
static MethodInfo GetMethod (MethodType method_type) {
Type layout = Type.GetType("UnityEditor.WindowLayout,UnityEditor");
MethodInfo save = null;
MethodInfo load = null;
if (layout != null) {
load = layout.GetMethod("LoadWindowLayout", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, new Type[] {typeof(string), typeof(bool)}, null);
save = layout.GetMethod("SaveWindowLayout", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, new Type[] {typeof(string)}, null);
}
if (method_type == MethodType.Save) {
return save;
}
else {
return load;
}
}
public static void SaveLayout(string path) {
path = Path.Combine(Directory.GetCurrentDirectory(), path);
GetMethod(MethodType.Save).Invoke(null, new object[] {path});
}
public static void LoadLayout(string path) {
path = Path.Combine(Directory.GetCurrentDirectory(), path);
GetMethod(MethodType.Load).Invoke(null, new object[] { path, false });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment