How to Use Editor Window

The manual and script reference don’t give specific enough instructions for me to get one to run in a game.

Script reference says “Editor windows are typically opened using a menu item.” But I don’t know what that means. I don’t see how to create a “menu item” or add one as a component. The sample script has the line

	// Add menu named "My Window" to the Window menu
	[MenuItem ("Window/My Window")]

but the script itself (see below) can’t be added to a game object because it generates the error “Can’t add script behavior MyWindow because it is an editor script. To attach a script it needs to be outside the ‘Editor’ folder.” But moving the script outside the editor folder and trying to put it on an object generates error “Can’t add script behavior MyWindow. The script needs to derive from MonoBehaviour!”

The sample script:

using UnityEngine;

using UnityEditor;

public class MyWindow : EditorWindow 

{

	string myString = "Hello World";

	bool groupEnabled;

	bool myBool = true;

	float myFloat = 1.23f;

	

	// Add menu named "My Window" to the Window menu

	[MenuItem ("Window/My Window")]

	

	static void Init () {

		// Get existing open window or if none, make a new one:

		MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));

	}

	

	void OnGUI () 

	{

		GUILayout.Label ("Base Settings", EditorStyles.boldLabel);

		myString = EditorGUILayout.TextField ("Text Field", myString);

		

		groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);

		myBool = EditorGUILayout.Toggle ("Toggle", myBool);

		myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);

		EditorGUILayout.EndToggleGroup ();

	}

}

Trying to trigger the static function from a different script like this one below generates the error “error CS0103: The name `MyWindow’ does not exist in the current context”

using UnityEngine;
using UnityEditor;
using System.Collections;

public class EditorGoScript : MonoBehaviour {
	
	// Add menu named "My Window" to the Window menu
	[MenuItem ("Window/My Window")]
	
	// Use this for initialization
	void Start () {
		MyWindow.Init();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Obviously I’m missing something here, something that will be obvious and simple.

Thank you for your help!

Hey Vimalakirti,

I was able to get an EditorWindow to popup through a menu command using your example script. Here’s what I did:

Create a new scripted named MyWindow
Place it in Assets/Editor (just create a new folder named ‘Editor’ on the root of your project in the Hierarchy panel)
Go to Window and select MyWindow

That popped up a new window which I could then populate if I wanted to. You don’t attach these to GameObjects or anything because these are extending editor functionality.

Are you looking to create an in-game menu system or is this to extend the Unity editor itself (which is what EditorWindow is used for)

Ohhh… I was looking to have window type gui elements in my game.

So it’s back to Angry Ant’s draggable gui then:
http://angryant.com/general/tipsandtricks/gui-drag-drop/

If you just need regular windows in GUI, check out GUI.Window - much less hazzle than using that example you linked. That was designed for drag-drop interfaces, not a replacement for GUI windows :slight_smile:

Such a very amazing link!

Emil,

Thanks for that tip. I’m using GUI.Window and GUILayout.Window now. Good tip! I’ll use it when I don’t need the true drag and drop functionality.

Skyline, Yes, isn’t that great work that Angry Ant did on those drag and drop windows?! Very cool stuff. It’s amazing how you can learn really great scripting skills from these guys who know how to use polymorphism and inheritance properly! I’ve made myself some drag and drop functions but only by using actual game objects triggered with OnMouseDown() functions. I should post those somewhere.

Bon voyage,
Vima