Creating Custom Editor Windows In Unity
Pushing Editor Scripts To The Limit; With Custom Editor Windows.
Let’s Do Some Hello Editor Window Scripting.
Alright, welcome back everybody. This is Part Two of our introduction to Editor Scripting in Unity, if you missed Part One you might want to check it out as a cushion before you get on to this. Link
Alright, so for this session we will looking at something that is not too complicated as far usual code is concerned but it's going to be a great introduction as far as creating a editor windows is concerned.
So What Will We Be Doing?
Alright! We are going to be building an editor window that will allow the user to select all objects that have a specific type of component attached to them. Some really cool functionality with just a little code.
Not Got It Yet? So the windows will allow our stressed developer to - with the click of a button, select all gameObejcts in the scene that have a specific component attached to them.
How Would This Be Useful. So you know those kinds of projects where you’re creating a custom AI pathfinder for unity and you get involved in placing a nodes for A*. Yeah you know what I mean, When you go to a scene of like a hundred objects labeled: node01,node02… it’s in these kind of situations that something like this becomes soo useful.
So while this tool might not look too exciting, it's definitely a practical one, and one that could save you a lot of headache and time especially in scenes where you have objects of the same repetitive type such as this one. So let's get to it mate!
The Node Class
For the purpose of the tutorial I have used the node class the came with the Brackeys project, however if your are not following along with that project you can simply create a node class by creating a new script call Node.cs and adding this code.
A Few Rules First;
In this specific tutorial I'm going to introduce the concept of the editor folder in unity. It's not that complicated, I promise. You know the way you always store your scripts in a “Scripts” folder? It's the same thing, simple as that.
But don't go celebrating too fast, the Editor folder isn't simply for guiding the programmer about where he can find all the scripts that he wrote himself, it's also about guiding Unity about how to treat your scripts. All scripts stored in an editor folder will not be compiled in the final game ie; they will stay in the Unity Editor only.
In fact scripts that stored in the editor folder are put in a whole separate Assembly meaning sometimes objects that you create inside Editor scripts are actually not accessible inside actual Scripts for the game and vice versa.
Anybody Miss MonoDevelop?
This image shows Visual Studio making the separation very clearly, it properly separates editor scripts from the actual scripts for the game and
and puts them into separate assemblies.
What does this mean?
This has its own pros and cons and will sometimes cause nightmares for your average Editor Scriptor but this as a topic is generally outside the scope of this tutorial. But definitely worth talking about another day.
That was some unnecessary stuff.
For now it's important to understand is that you should create a new folder in your project called Editor (with the capital E). And place our code in there.
What is that?
For this tutorial I used a tutorial project from the Brackeys website. He's an amazing creator of tutorials on YouTube and has some brilliant playlists if you're trying to learn programming in Unity. For this however we will simply be using his Tower Defense project which you can grab here.
I will mention that it's not absolutely necessary for this tutorial but I find it nice to have screenshots of unity that actually have content in them( code alone isn't too amusing to the eye), not to mention it had a node based pathfinding system in it which is brilliant for this tool.
Creating An Editor Window.
Creating a menu item
This by far being the easiest part in this entire tutorial - allows us to create an option in the title menu in unity which the developer can use to open up our custom editor window
At the same time we'll create a function that opens up the editor window for obvious reasons this function needs to be static and public. Let's take a look at the current code.
Next inside of our Instantiate function we will either create or get an active instance of our editor window using the GetWindow() method and passing in a type of a our window, to clarify this function either gets an active instance of our editor window or creates one.(this functionality is very important as it allows us not to need to manually create instances of our editor window). Here is the code for this;
Next we create our OnGUI function and use it as if we had forgotten that we were editor scripting.
So fast we create a new button.
And then game logic comes into action as this is outside of the scope of this tutorial I will not give too much of an explanation except for a highlight of the fun that I had playing with lists and arrays and how the unity editor works mainly with arrays ( at least as far as game object selection is concerned.).
Let's Do This Real Quick.
So we need a list of all the node objects in the scene, and for that we use FindObjectsOfType<Node>();
To store this list we use an array as we are typically getting an array, this by itself caused a bit of a problem later on as we will see.
Next we create a list of GameObjects and loop through our array of nodes adding each game object to our list, next we convert the list to an array and make that the selected game objects in unity.
Finally I'll point out the array and list limbo that's going on here as Unity uses arrays which have less requirements but are harder to handle and luck some functionalities that lists have, hence I prefer to work in lists and then, if need be convert them to arrays.
A Simpler Version Of My Code.
Now this code will not create the editor window that I showed earlier, it will however create an editor window with a button that allows you to select all game objects that have a node component attached to them.
Try it out!
And now it's probably time for you to try and create more functionality for this window a good way to go would probably be to try and change the colors of the buttons or to add buttons that allow you to select different types of components.
The complete source code for this window is available for download if you would want to use it in your own project, please take it and expand on it. And don't forget to join us for the next tutorial where we will be taking a venture into scriptable objects in unity. Thank you and see you next time.