Search Unity

Question EditorGUIUtility.Load

Discussion in 'Code Editors & IDEs' started by Ndogg27, Feb 2, 2024.

  1. Ndogg27

    Ndogg27

    Joined:
    Feb 3, 2023
    Posts:
    19
    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. ;(

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.IO;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. [InitializeOnLoad]
    7. public static class LayoutSwitcher
    8. {
    9.     private const string playLayout = "NdoggTesting";
    10.     private const string editorLayout = "Ndogg";
    11.  
    12.     static LayoutSwitcher()
    13.     {
    14.         EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    15.     }
    16.  
    17.     private static void OnPlayModeStateChanged(PlayModeStateChange state)
    18.     {
    19.         if (state == PlayModeStateChange.EnteredPlayMode)
    20.         {
    21.             SwitchToLayout(playLayout);
    22.         }
    23.         else if (state == PlayModeStateChange.ExitingPlayMode)
    24.         {
    25.             SwitchToLayout(editorLayout);
    26.         }
    27.     }
    28.  
    29.     private static void SwitchToLayout(string layoutName)
    30.     {
    31.         var layout = EditorGUIUtility.Load("Layouts/" + layoutName + ".wlt") as EditorWindow;
    32.         if (layout != null)
    33.         {
    34.             layout.Show();
    35.             EditorApplication.ExecuteMenuItem("Window/Layouts/" + layoutName);
    36.         }
    37.         else
    38.         {
    39.             Debug.LogError("Layout " + layoutName + " not found!");
    40.         }
    41.     }
    42. }
    upload_2024-2-2_0-33-6.png
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,921
    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.
     
  3. Ndogg27

    Ndogg27

    Joined:
    Feb 3, 2023
    Posts:
    19
    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.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,921
    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.
     
  5. Ndogg27

    Ndogg27

    Joined:
    Feb 3, 2023
    Posts:
    19
    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.

    Code (CSharp):
    1.  
    2. private static void OnPlayModeStateChanged(PlayModeStateChange state)
    3. {
    4.     if (state == PlayModeStateChange.ExitingEditMode)
    5.     {
    6.         EditorApplication.ExecuteMenuItem("Window/Layouts/NdoggTesting");
    7.     }
    8.     else if (state == PlayModeStateChange.EnteredEditMode)
    9.     {
    10.         EditorApplication.ExecuteMenuItem("Window/Layouts/Ndogg");
    11.     }
    12. }
    13.  
     
    CodeSmile likes this.