Jump to content

  • Log In with Google      Sign In   
  • Create Account


Like
14Likes
Dislike

Scripting Custom Windows in Unity3D

By Casey Hardman | Published Aug 20 2013 11:43 AM in Game Programming
Peer Reviewed by (jbadams, Josh Vega, Dave Hunt)

unity3d editor editor window window gui

Unity allows you to extend its interface by scripting your own custom windows, which will be draggable and re-sizable like the other windows in Unity (i.e. Project, Scene, Hierarchy, Inspector).

I'm going to teach you the basics of making your own windows.

You will need:
  • Unity3D
  • Something to edit scripts with. If you have Unity installed, you should at least have the built-in editor, UniSciTE, installed.
  • You should at least know the very basics of Unity and scripting with Unity.

Creating a Window


Let's start by getting our new window to show up. Unity makes this rather simple.

Create a new project or open an existing project (whichever you prefer).

In your Project window (by default, it's at the bottom of the screen), click the Create button and then click 'Folder'. You can also right-click anywhere in the Project window, hover your mouse over 'Create', and then click 'Folder'.


Attached Image: CreateFolder.png


In order to script a window, you have to put your script files in a folder that's named "Editor". If it's not in a folder named Editor, it cannot access the classes and functions it needs to behave like a window. So name the new folder you created 'Editor'. If it's not already being renamed, click twice on the folder name to begin renaming it.

Once the folder is properly named, right-click on it, select Create, and then the type of script you want to make (JavaScript, C#, or Boo). Name this new script "MyWindow".

Open the script with your text editor.

Note:  
JavaScript users may not know this: every script file in Unity is actually a class that extends the MonoBehaviour class. Unity 'hides' the class declaration from you if you use JavaScript (but it doesn't do this for Boo or C#), but it lets you declare it yourself if you want to.


We want our new script to inherit from the EditorWindow class, not MonoBehaviour, because we don't need to place this script on GameObjects; we just need it for this window.

Declare your class like this:

//C Sharp:
using UnityEngine; //This should be in your script file already.
using UnityEditor; //This won't be in your script file by default, so put it there.  It lets us access all of Unity's methods and classes for making windows.

public class MyWindow:EditorWindow //Name your class the same thing you named your script file, and make sure it inherits from EditorWindow instead of MonoBehaviour (it will inherit from MonoBehaviour by default!)
{
	
}

//JavaScript:
import UnityEditor; //This won't be in your script file by default, so put it there.  It lets us access all of Unity's methods and classes for making windows.

public class MyWindow extends EditorWindow //Name your class the same thing you named your script file, and make sure it extends EditorWindow instead of MonoBehaviour (it will extend MonoBehaviour by default!)
{

}

Now we'll add an option to create our window. Put this function in your window class (the one named MyWindow that we just made):

//C Sharp:
[MenuItem ("Window/My Window")] //This is the place that the option to create the window will be added.
public static void ShowWindow() //Don't change the name of the function
{
	EditorWindow.GetWindow(typeof(MyWindow)); //If you disobeyed this article and named your class something else, replace the 'MyWindow' in this line with that name.
}

//JavaScript:
@MenuItem("Window/My Window") //This is the place that the option to create the window will be added.
static function ShowWindow() //Don't change the name of the function
{
	EditorWindow.GetWindow(MyWindow); //If you disobeyed this article and named your class something else, replace the 'MyWindow' in this line with the actual name.
}

Now you can open your window by clicking on the 'Window' menu at the top of the screen, then finding 'My Window' and clicking on that. Your window won't have anything in it yet.

Displaying Things in the Window


Now that your window is showing up, let's get it to show things.

This kind of code should go in the OnGUI() function in your class.

Usually, you'll be using editor functions that you can find in the EditorGUI and EditorGUILayout classes, which we have access to since we're inheriting from EditorWindow.

Let's make some fields that the user can input into. Here's the code, including all of the previous code:

//C Sharp:
using UnityEngine;
using UnityEditor;

public class MyWindow:EditorWindow
{
	//Let's declare some variables that we'll use later:
	public string textField = "Text";
	public int intField = 2;

	void OnGUI() //This is where we'll put all of our code that the window should run
	{
		textField = EditorGUILayout.LabelField("Input some text:",textField); //This will display the first string, followed by a box that the user can type characters into
		intField = EditorGUILayout.IntField("Input some numbers:",intField); //This will display the first string, followed by a box that the user can type numbers into
	}
	
	//Displaying the Window:
	[MenuItem ("Window/My Window")]
	public static void ShowWindow()
	{
		EditorWindow.GetWindow(typeof(MyWindow));
	}
}

//JavaScript:
import UnityEditor;

public class MyWindow extends EditorWindow
{
	//Let's declare some variables that we'll use later:
	public var textField:String = "Text";
	public var intField:int = 2;

	function OnGUI() //This is where we'll put all of our code that the window should run
	{
		textField = EditorGUILayout.TextField("Input some text:",textField) as String; //This will display the first string, followed by a box that the user can type characters into
		intField = EditorGUILayout.IntField("Input some numbers:",intField); //This will display the first string, followed by a box that the user can type numbers into
	}
	
	@MenuItem("Window/My Window")
	static function ShowWindow()
	{
		EditorWindow.GetWindow(MyWindow);
	}

}

Conclusion


Now you're prepared to make your own windows in Unity. Once again, make sure you always place your window scripts in an 'Editor' folder, and be sure to inherit from EditorWindow instead of MonoBehaviour.

Here are some miscellaneous notes about scripting Unity windows:
  • The Input.mousePosition variable is not updated when the game is not playing, and is useless for editor windows. If you want to get the mouse position, you'll have to use Event.current.mousePosition in an OnGUI() function instead.
  • Variables that aren't public will reset to their default values when your project refreshes. This means if you edit a script or add a new file to your Assets folder, every variable that is not public will revert back to what you declared it as. For example, if a variable is set to 2 by default, but during your use of the window, you set that variable to 5, then it would reset back to 2 every time the project refreshes.

Article Update Log


May 10 2013: Initial release



License


GDOL (Gamedev.net Open License)




Comments

Might I also recommend, when dealing with mouse events such as mouse up & mouse down, look into using the Event.rawType vs. the Event.type, which will allow you to do things like capture a mouse up when the mouse is not currently over the editor window.

I want it, however the price for a solo developer just wanting to build a prototype scared me away, the giro here is just £140 for two weeks, and as I am currently out of work I really can't afford to buy. Credit is due to the fantastically talented team, I'd just say, please do not make the word 'unity' into a metaphor for 'monopoly' if only for the sake of beautiful code (or something), maybe give us some real goods to use for free before we consider a release?


Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.




PARTNERS