So I am new to working with Editor Windows. I have made one that appeared to work fine, but recently I found out that if I dock it and exit Unity, when I load back up the docked window still shows but it has no contents and it is throwing null reference exceptions. Here is code for one of the windows. I'm sure if I get help with this one I should be able to figure out the others. Code (CSharp): using SubsonicGames.EditorUtils; using SubsonicGames.MiscUtils; using UnityEditor; using UnityEngine; public class ssEditorTools : EditorWindow { private static ssEditorTools window; private float buttonVertSize = 25f; [MenuItem("SS Tools/Editor Extensions #%y")] private static void Init() { window = (ssEditorTools)GetWindow(typeof(ssEditorTools), false, "SS Editor Tools"); window.minSize = new Vector2(300f, 300f); window.maxSize = new Vector2(300f, 300f); window.Show(); } private void OnInspectorUpdate() { Repaint(); } private void OnGUI() { ssEditorGUI.SetSSEditorBG(window); GUILayout.BeginArea(new Rect(0, 0, window.position.width, window.position.height)); { GUILayout.FlexibleSpace(); if (ssEditorGUI.CreateCeneteredButton("Setup New Project", "", GUI.skin.GetStyle("Button"), GUILayout.MaxWidth(window.position.width - 25), GUILayout.MinHeight(buttonVertSize))) { ssSetupProject.GenerateProject(); } ssEditorGUI.RememberColors(); if (FindEditorWindow<ssAutoSaveOptions>()) { ssEditorGUI.SetColor(ssEditorGUI.redLt); } else { ssEditorGUI.SetColor(ssEditorGUI.greenLt); } if (ssEditorGUI.CreateCeneteredButton("Save Options", "", GUI.skin.GetStyle("Button"), GUILayout.MaxWidth(window.position.width - 25), GUILayout.MinHeight(buttonVertSize))) { ssAutoSaveOptions.OpenCloseWindow(); } ssEditorGUI.ResetColor(); ssEditorGUI.RememberColors(); if (FindEditorWindow<ssToolbar>()) { ssEditorGUI.SetColor(ssEditorGUI.redLt); } else { ssEditorGUI.SetColor(ssEditorGUI.greenLt); } if (ssEditorGUI.CreateCeneteredButton("Quick Bar", "", GUI.skin.GetStyle("Button"), GUILayout.MaxWidth(window.position.width - 25), GUILayout.MinHeight(buttonVertSize))) { ssToolbar.OpenCloseWindow(); } ssEditorGUI.ResetColor(); GUILayout.FlexibleSpace(); } GUILayout.EndArea(); } bool FindEditorWindow<TWindowType>() where TWindowType : EditorWindow { var found = false; TWindowType[] windows = Resources.FindObjectsOfTypeAll<TWindowType>(); if (windows != null && windows.Length > 0) { found = true; } return found; } } The null reference is referencing this piece of code in the ssEditorGUI class Code (CSharp): public static void SetSSEditorBG(EditorWindow window) { GUI.DrawTexture(new Rect(0, 0, window.position.width, window.position.height), Resources.Load("ssEditorBG") as Texture2D); }
So I keep digging more and it looks like everything I come across with this error keeps saying you have to have the GUI.DrawTexture in OnGUI(). I do have it in OnGUI() in the main file, so I am confused why it doesn't work. Is OnGUI not activated when Unity opens?
Well just to close this up, I did eventually find the answer myself. I was not properly storing a reference of the editor window for when Unity shut down, so it basically was null each time unity came back up. I changed the beginning code to this on all the windows, and now it works like a charm. Code (CSharp): private static ssToolbarOptions _instance; #region INITIALIZATION public static void Init() { GetWindow(typeof (ssToolbarOptions), false, "Options"); } private void OnEnable() { _instance = this; SceneView.RepaintAll(); } public static void OpenCloseWindow() { if (_instance != null) { _instance.Close(); } else { Init(); _instance.minSize = new Vector2(180f, 200f); _instance.maxSize = new Vector2(180f, 200f); _instance.Show(); } } #endregion