An Intro To Unity Editor Scripting
What Is This?
The image above is a screenshot of Shadergraph, and in case you missed it, there was a time when Shadergraph was one of the most talked about Unity assets; like literally top of the Asset Store.
This is a typical example of how powerful Editor Scripting can be; Unity allows you to extend the functionality of the editor for your specific project and goals.
How useful would this be?
Let's run through a quick; and in no way comprehensive list of what you could gain from editor scripting:
For starters you could save time on your projects. Current Unity assets from the store save Developers over 5 months of development time, it's much easier, and faster to buy a $20 Unity tool than have to pay a team of 5 Developers to make it in-house.
Earn money as an asset store developer. Unity announced last year at a conference that they had over 20,000 developers globally. This is a huge list of people that visit the Unity Asset Store at least once a month, imagine what would happen if your asset was on the first page. I don't think this point needs any more explanation.
Simplify things for some of your developers. I know from experience how artists react to a cluttered Unity Inspector, you don't want to see it. So why not make them some editor extensions to simplify this?
The list goes on and on; as far as your imagination, one thing is for sure though; NOTHING IS MORE POWERFUL THAN CUSTOMIZATION!
Let’s Write Our First Editor Script!
In this short tutorial, we will create a point in Unity that draws a cube to show its location, this can be used for creating debugger points for artists working on way-point systems; such as shown below. Now STOP! Have a seat; this is nothing too complicated.
Interested? Well, let's get started. Now, just for clarity I want be mentioning the usual “open a project, click here, click their” kind of thing, I suspect that by the time you are reading this, you at least have an idea of the fundamental workings of Unity.
Let's get started:
So first of all let's create a new empty Game Object in a new Unity Project.
Now, in the scene view you can easily see the position of the game object while it is selected but when deselected. It's essentially not there.
Now, under normal circumstances this is okay; however in In some cases you might want to be able to see an object even while it's not selected. This is where Editor Scripting comes in.
So let's do this, start by creating a new script you can call it whatever you want, I've called mine “EditorTest.cs” (Without the quotes).
And add it to the empty game object.
Next, in your preferred IDE let's edit the script.(Almost forgot Unity removed MonoDevelop; it was a sad ending)
To draw our editor components we use a class called OnDrawGizmos(), this class is recognized by unity just like Start() and Update().
Next we call the Gizmos class and pass in our position and Vector3.one as the scale. This draws a cube at the position of our gameObject. Nothing left to do now but save and return to Unity.
So there we are, now the object can be seen whether it is selected or not which can be helpful to at least one or two artists on the team, if you get what I mean. This wasn't too complicated, was it?
Finally this was just one of the simplest Editor Scripts you can write in unity, this spectrum, unlike light; is endless and this was just to get you started, join us for the next session were we will be looking for some more complicated work to do using Editor Scripts or check out the Unity Documentation for the topic. Unity Editor Scripting.
Hope to hear from you.