Last active
November 2, 2023 21:04
-
-
Save LotteMakesStuff/9c623cfa51e256baf183c357450cdbc8 to your computer and use it in GitHub Desktop.
Simple tool for importing editor layout files (.wlt files) from the asset folder. this is cool cos you can then share layouts with your team via source control
This file contains hidden or 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
// put me in an Editor folder | |
using System.IO; | |
using UnityEditor; | |
using UnityEditorInternal; | |
using UnityEngine; | |
public static class LayoutUtils | |
{ | |
// unity stores layouts in a path referenced in WindowLayout.LayoutsPreferencesPath. | |
// Unfortunately, thats internal - well just creat the path in the same way they do! | |
// github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/WindowLayout.cs#L980 | |
static string layoutsPreferencesPath => Path.Combine(InternalEditorUtility.unityPreferencesFolder, "Layouts"); | |
[MenuItem("Assets/Import Editor Layout", priority = 10000)] | |
static void ImportEditorLayout() | |
{ | |
Debug.Log("Attempting to import layout: " + Selection.activeObject.name); | |
// install the layout file by copying it to the layout folder | |
string sourcePath = AssetDatabase.GetAssetPath(Selection.activeObject); | |
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/WindowLayout.cs#L1088 | |
string destinationPath = Path.Combine(layoutsPreferencesPath, Selection.activeObject.name + ".wlt"); | |
File.Copy(sourcePath, destinationPath, true); | |
// instruct unity to reload layouts | |
InternalEditorUtility.ReloadWindowLayoutMenu(); | |
} | |
[MenuItem("Assets/Import Editor Layout", priority = 10000, validate = true)] | |
static bool ValidateImportEditorLayout() | |
{ | |
// we only work on a single selected asset... | |
if (Selection.objects.Length != 1) | |
return false; | |
// lets check if the asset is a layout file | |
string path = AssetDatabase.GetAssetPath(Selection.activeObject); | |
return path.EndsWith(".wlt"); | |
} | |
[MenuItem("Window/Open Editor Layouts folder", priority = 10000)] | |
static void OpenlayoutsFolder() | |
{ | |
Debug.Log("Attempting to import layout: " + Selection.activeObject.name); | |
EditorUtility.RevealInFinder(layoutsPreferencesPath+"/default"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Lotte,
This was very useful. Thank you!
I got a NullReferenceException from the OpenLayoutsFolder() Debug call, and the Assets menu refused to play nice, so I made a few small tweaks to remove the NRE and move the MenuItems to the Windows menu.
Thank you again,
– Jeremy