I'm having a problem while creating a custom EditorWindow

The problem is, that I cant store objects when selecting them through the custom window.
Heres the code:

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

public class ShaderSetupEditor : EditorWindow
{   
    public Cubemap defaultCube = null;   
    public float exposure;
   
    [MenuItem("X window/Setup")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(ShaderSetupEditor));
    }
   
    void UpdateShaders()
    {       
        if(defaultCube) {
            Shader.SetGlobalTexture("_Cube", defaultCube);
        }
    }
   
    void OnGUI()
    {
        EditorGUILayout.LabelField("Chose a default cubemap for the shaders.");   
        EditorGUI.BeginChangeCheck();
        {           
           string commandName = Event.current.commandName;
           if (commandName == "ObjectSelectorUpdated") {
               defaultCube = EditorGUIUtility.GetObjectPickerObject () as Cubemap;
               Repaint ();
           }
            EditorGUILayout.ObjectField (defaultCube, typeof(Cubemap), true);
        }       
        UpdateShaders();
    }
}

Any sugestions?

Why are you doing

  • BeginChangeCheck without an EndChangeCheck?
  • if(commandName === … }

Wouldn’t it be easier to just have

EditorGUI.BeginChangeCheck();
defaultCube = (Cubemap)EditorGUILayout.ObjectField (defaultCube, typeof(Cubemap), true);
if(EditorGUI.EndChangeCheck())
    UpdateShaders();

instead

1 Like

Yes the code was incomplete in terms of unity’s functions, because I tried various methods and this method didnt work.

Also I think I’m abandoning cusom EditorWindows because they get destroyed once they are closed or play is pressed, so all sorts of weird stuff happened, I switched to the default prefab method, where you have a custom inspector. This method atleast works like it should.

Custom editors are destroyed over now and then as well. What is the “all sorts of weird stuff” that you are referring to?

If you need some piece of code that gets executed without any editor window open, you should look into [InitializeOnLoad]