Skip to content

Instantly share code, notes, and snippets.

@marcolink
Last active April 9, 2021 06:02
Show Gist options
  • Save marcolink/19c4cf98bdb1cccd10ea75e1f8ac6cce to your computer and use it in GitHub Desktop.
Save marcolink/19c4cf98bdb1cccd10ea75e1f8ac6cce to your computer and use it in GitHub Desktop.
Unity EditorWindow

Unity EditorWindow

Resources

Undocumented

Will stop GUI calling layout methods until next frame.

EditorGUIUtility.ExitGUI();

Best Practice

Serialization: Every time you start/stop playmode, or you load/unload a scene, the editor serializes/deserialzes the editor(windows) itself. EditorWindow provides a flag (HideFlags) to avoid this behavior. Once you start splitting your code in different classes, you have to ensure all your code is serializable. You can either do that by adding [Serializable] to your class, or derive from ScriptableObject. If you derive from ScriptableObject, ensure you set the hideFlags to HideFlags.DontSave.

Example

using System;
using UnityEditor;
using UnityEngine;

namespace wooga.WDK.Editor
{
    public class TestWindow : EditorWindow
    {
        public SubClass myInstance;

        [MenuItem("Test Window")]
        private static void ShowWindow()
        {
            GetWindow(typeof(TestWindow));
        }

        void Awake()
        {
            myInstance = new SubClass();
            myInstance.instanceName = "TestName";
        }

        private void OnGUI()
        {
            GUILayout.Label(myInstance.instanceName);
        }
    }
    
    [Serializable]
    public class SubClass 
    {
        public string instanceName;
        
    }
}

In case you want to serialize private fields, make sure you add [SerializeField]

[Serializable]
public class SubClass 
{
    public string instanceName;
    [SerializeField] private string instanceDescription;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment