Unity3d :: Editor vs EditorWindow

Hi,

When getting into writing custom editors or extending the functionality of the Unity 3D user interface, looking at the API for the first time (Editor API) might have you scratching your head. Reading a bit into the docs, you start to see a very subtle difference between the two classes Editor and EditorWindow.

My first instinct was that Editor was the base abstract class of EditorWindow, not the case.

The Editor decorates an existing Inspector, the EditorWindow is an entirely new Inspector with your custom editor contained within with custom controls to capture user information.

Editor

The Editor class allows you to "attach" user interface controls onto an existing Inspector. The Editor class contains a public variable target that holds the reference to the current selection in the scene. The UnityEngine knew that the Component selected in the scene was of the type CustomEditor defined in the editors class attribute.

Looking at the example below, MyBehaviorScript would be a javascript or C#(extending MonoBehaviour) script that contained behaviors to add onto a GameObject instance in the scene. Using the CustomEditor attribute, the UnityEngine knows to call OnInspectorGUI() on this class when an instance of MyBehaviorScript is found in the GameObject's component collection.

So in laymans terms, the Editor class dynamically adds controls to the Inspector when a selection occurs in the Scene Editor.

using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor(typeof(MyBehaviorScript))]
 
public class MyEditor : Editor 
{
	override public void OnInspectorGUI()
	{
		Debug.Log("OnInspectorGUI()");
	}
}

EditorWindow

The EditorWindow class allows you to create new Inspector windows that can float free or be docked as a tab, just like the native windows in the Unity 3D interface. The EditorWindow usually have menu items assigned within the declaring class.

Using;

[MenuItem ("Window/My EditorWindow")]

will create a menu item in the Window main menu. When a user selects this menu item, the new Inspector window will be created and if the Inspector exists will get focus.

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class MyEditorWindow : EditorWindow 
{
 
	[MenuItem ("Window/My EditorWindow")]
	static void Init ()
	{
		// Get existing open window or if none, create it
 
		MyEditorWindow window = (MyEditorWindow)EditorWindow.GetWindow(typeof(MyEditorWindow));
	}
 
	void OnGUI ()
	{
		// Called when the user interface is created
		GUILayout.Label ("Hellow World", EditorStyles.boldLabel);
	}
}

Remember that the Editor class is populated with it's target as the current selection in the scene? You may ask, well since this class has nothing to do with Editor and the target property, how do you know what is selected in the scene.

Answer; The EditorWindow class receives OnSelectionChange() messages any time the selection changes in the scene editor. You can action the selection, save the selection or load a selection you saved. You will access the current selections in the scene with Selection.instanceIDs;.

	int[] selectionIDs;
 
	void OnSelectionChange()
	{
		selectionIDs = Selection.instanceIDs;
	}

The integers point to scene object instance ids. You could simply load the int array back into the Selection.instanceIDs array.

Selection.instanceIDs = ids;

Mike