- Home /
How to create EditorWindow on game load
Hi,
I am trying to create a custom EditorWindow at the start of the game. I want the window to be created without having the user to select if from the 'window' menu.
I figured out I should use [InitializeOnLoad] - http://docs.unity3d.com/Documentation/Manual/RunningEditorCodeOnLaunch.html, and create the window in the static constructor.
Here is my code:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class SupportScript : EditorWindow {
static EditorWindow window;
static SupportScript()
{
window = ScriptableObject.CreateInstance(typeof(SupportScript) ) as EditorWindow;
window.Show();
Debug.Log("Window showed");
}
void OnGUI()
{
....
}
}
When I launch the game, I get bunch of errors such as:
Instance of ContainerWindow couldn't be created because there is no script with that name.
System.NullReferenceException: Object reference not set to an instance of an object
Failed to destroy editor windows: #1
The first error is in the line: Window.Show();
But, if I am changing something in the script (or any other script) and so the scripts recompile, everything works fine. So the problem is only on the first launch.
Does any one know any other method to create a window on editor launch? I would appreciate any kind of help in this issue.
Many thanks in advance,
Anton
Answer by Bunny83 · Nov 13, 2013 at 03:34 PM
Simply use the update delegate (shown here) as a one-time-trigger. That should delay the creation until the editor runs it's first update.
Something like this:
[InitializeOnLoad]
class StartupHelper
{
static StartupHelper()
{
EditorApplication.update += Startup;
}
static void Startup()
{
EditorApplication.update -= Startup;
// Do your stuff here,
}
}
$$anonymous$$y window constantly resets, and is rendered unusable, since I use input fields, which for some reason reset if you click away.