Last active
March 21, 2025 12:36
-
-
Save karl-/68fe2d5cbfe60456867b21aaa8da30c3 to your computer and use it in GitHub Desktop.
Set Unity Layout on Play Mode State Change
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
private const string LastLayout = "Library/LastEditModeLayout.wlt"; | |
private delegate void SaveWindowLayoutDelegate(string path); | |
private static SaveWindowLayoutDelegate _saveWindowLayout; | |
[InitializeOnLoadMethod] | |
private static void Init() | |
{ | |
var windowLayoutType = typeof(Editor).Assembly.GetType("UnityEditor.WindowLayout"); | |
var save = windowLayoutType.GetMethod("SaveWindowLayout", BindingFlags.Public | BindingFlags.Static); | |
_saveWindowLayout = (SaveWindowLayoutDelegate)Delegate.CreateDelegate(typeof(SaveWindowLayoutDelegate), save); | |
EditorApplication.playModeStateChanged += evt => | |
{ | |
if (!IsActive(StudioMode.Simple)) | |
return; | |
switch (evt) | |
{ | |
case PlayModeStateChange.ExitingEditMode: | |
_saveWindowLayout.Invoke(LastLayout); | |
break; | |
case PlayModeStateChange.EnteredPlayMode: | |
EditorApplication.delayCall += () => UnityEditor.EditorUtility.LoadWindowLayout(PlayModeLayout); | |
break; | |
case PlayModeStateChange.EnteredEditMode: | |
UnityEditor.EditorUtility.LoadWindowLayout(LastLayout); | |
break; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment