EditorGUIUtility.Load

I don’t understand what I am doing wrong. I have messed with the path for this function a dozen different ways and it never loads my layout.

What this class is suppose to do is switch the Unity layout between two of my custom layouts for play mode and editor mode.

Could someone please help? I thought I was adding a basic feature and now I’ve wasted a bunch of time on this. ;(

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public static class LayoutSwitcher
{
    private const string playLayout = "NdoggTesting";
    private const string editorLayout = "Ndogg";

    static LayoutSwitcher()
    {
        EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    }

    private static void OnPlayModeStateChanged(PlayModeStateChange state)
    {
        if (state == PlayModeStateChange.EnteredPlayMode)
        {
            SwitchToLayout(playLayout);
        }
        else if (state == PlayModeStateChange.ExitingPlayMode)
        {
            SwitchToLayout(editorLayout);
        }
    }

    private static void SwitchToLayout(string layoutName)
    {
        var layout = EditorGUIUtility.Load("Layouts/" + layoutName + ".wlt") as EditorWindow;
        if (layout != null)
        {
            layout.Show();
            EditorApplication.ExecuteMenuItem("Window/Layouts/" + layoutName);
        }
        else
        {
            Debug.LogError("Layout " + layoutName + " not found!");
        }
    }
}

9619499--1365569--upload_2024-2-2_0-33-6.png

What makes you think that a layout file is equivalent to an EditorWindow class instance?
I don’t think that’s what it is. Omit that cast and check if layout is non-null and if so, what type it actually is.

I’ve never messed with the editor before, so maybe my approach is all wrong. Removing that did fix the load issue, but I don’t know how to make the layout switch. The layout files are just Default Assets. ExecuteMenuItem now gives me an error saying the Window/Layouts/… doesn’t exist.

Default Asset means there is no importer for it. Those are .wlt files, right? Unity would have to provide a method to load and apply those .wlt files programmatically. Check if something like that exists, including any internal methods that you may be able to call via reflection. If none of that exists, changing layouts is not scriptable.

Yes, they are .wlt files. I’m struggling to find anything useful on this. But, I happened to notice that it is actually working when exiting play mode, but with an error. So, I used the below instead of EnteredPlayMode and ExitingPlayMode which apparently is ok and got rid of EditorGUIUtility.Load. Figured it out, but F***! ~10 simple lines of code took me forever… Thanks for helping.

private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
    if (state == PlayModeStateChange.ExitingEditMode)
    {
        EditorApplication.ExecuteMenuItem("Window/Layouts/NdoggTesting");
    }
    else if (state == PlayModeStateChange.EnteredEditMode)
    {
        EditorApplication.ExecuteMenuItem("Window/Layouts/Ndogg");
    }
}
1 Like