First up, an example of 'not very helpful' formatting.
So imagine the following scenario: You're a designer. You've just got your hands on unity and have minimal programming knowledge.
You've heard good things about a particular program on the asset store, and decide to download it and import it into your project for some tests.
Then imagine seeing this mess when you drag the script onto an object:
You've heard good things about a particular program on the asset store, and decide to download it and import it into your project for some tests.
Then imagine seeing this mess when you drag the script onto an object:
I feel bad having to write that script. It was.. To say the least, painful writing it in such a fashion.
But just imagine seeing something so poorly laid out and not user friendly. Imagine even paying for a released product and there being the chance it could be laid out that way.
That is where extending the editor for you own benefit can help your code and what you see in the editor look better than the above.
But just imagine seeing something so poorly laid out and not user friendly. Imagine even paying for a released product and there being the chance it could be laid out that way.
That is where extending the editor for you own benefit can help your code and what you see in the editor look better than the above.
Secondly, an example of good formatting for the above.
For starters, what we need to do is show you exactly what that code was for the above script so you can get an idea of just how broken it is, and how we fix it.
BadScript.cs
So. The above is definitely not for the faint of heart.
But that's alright, because we're going to fix it.
So why don't we begin by clearing it up? That'd be a start.
But that's alright, because we're going to fix it.
So why don't we begin by clearing it up? That'd be a start.
BadScript.cs
Okie dokie. Now it's laid out correctly. Notice that I got rid of the ranges on the rotation. That'll come into play later when we actually extend the editor and add a couple o' conditions.
Now we'll spruce it up with some formatting tags and see how it looks in Unity.
Now we'll spruce it up with some formatting tags and see how it looks in Unity.
BadScript.cs
And with that, cautious planning could prevent the disaster that was the initial script.
But even then...
What if we take it a little further?
But even then...
What if we take it a little further?
Extending the Unity Editor and doing some gobbledygook.
So you're content with how the above looks. Sure. But what if you wanted to handle positional data for multiple types? What if they needed different values all together if they're of a different type?
What if you're a lazy programmer like me and want a way to do it that is simple and easy to implement?
Then keep on readin', boyo. We'll get there.
So we'll start by creating a new folder in Unity, outside of resources and all that stuff called 'Editor'. Then inside it, we'll place a file called BadScriptEditor. It'll be a C# file like the BadScript file, but this one will derive from Editor instead of MonoDevelop. Not only that, we need to tag it as a custom editor for the BadScript, saying that it can edit multiple objects; the thing we need to be able to have multiple object states and types in the editor.
The following is the script I have to extend the BadScript C# file. But it doesn't do much just yet; we have to add Serialized Properties to actually hold the values from the serialized object.
What if you're a lazy programmer like me and want a way to do it that is simple and easy to implement?
Then keep on readin', boyo. We'll get there.
So we'll start by creating a new folder in Unity, outside of resources and all that stuff called 'Editor'. Then inside it, we'll place a file called BadScriptEditor. It'll be a C# file like the BadScript file, but this one will derive from Editor instead of MonoDevelop. Not only that, we need to tag it as a custom editor for the BadScript, saying that it can edit multiple objects; the thing we need to be able to have multiple object states and types in the editor.
The following is the script I have to extend the BadScript C# file. But it doesn't do much just yet; we have to add Serialized Properties to actually hold the values from the serialized object.
BadScriptEditor.cs
So what we have access to now, since we are deriving from Editor, is access to the OnInspectorGUI function.
This is super handy for tools development, right? You can perform object specific functions way before runtime.
For example, in one of my earlier games that I haven't posted on this site yet, I have a bunch of tiles. And the editor I'm using to change what they are is just an enum; when I change it to a red tile, the script will step into a switch and handle setting the image and a bunch of other values in the inspector so that I don't have to.
So we'll add some serialized properties in the editor script and give them appropriate values in the 'OnEnable' function.
This is super handy for tools development, right? You can perform object specific functions way before runtime.
For example, in one of my earlier games that I haven't posted on this site yet, I have a bunch of tiles. And the editor I'm using to change what they are is just an enum; when I change it to a red tile, the script will step into a switch and handle setting the image and a bunch of other values in the inspector so that I don't have to.
So we'll add some serialized properties in the editor script and give them appropriate values in the 'OnEnable' function.
BadScriptEditor.cs
So now that we have these values from the object we have the script attatched to in the editor, what we can do it handle type specific stuff in the OnInspectorGUI function. What we're going to do is go back into our regular BadScript.cs file and add a small enumerator and member variable. Public, of course. Don't forget to add them to your list of serializedObjects in your Editor C# file.
BadScript.cs
Now we have the enum and we can access it, what we do is go back to the BadScriptEditor C# file and make sure we have everything set up correctly using serialized properties.
Once we're all good and proper, we'll add the following to the OnInspectorGUI function.
Once we're all good and proper, we'll add the following to the OnInspectorGUI function.
BadScriptEditor.cs
So if you're not big on reading comments in code, I'll glaze over it. So what's happening is the following:
I click on the object in the scene.
It steps into this function and execute the code.
In this code, I'm handling types for different range modes.
Depending on range mode is what is seen in the editor.
Simple, easy to implement and easier to maintain and keep going along with your expanding project. Just make an editor and throw in some variables. Maybe even cut down on the amount of variables you have all together and be a cool cat like that; you can store variables in the editor, so you know. That opens up a wide world of possibilities.
Here's a pic of the differences to the slider/non slider variants.
I click on the object in the scene.
It steps into this function and execute the code.
In this code, I'm handling types for different range modes.
Depending on range mode is what is seen in the editor.
Simple, easy to implement and easier to maintain and keep going along with your expanding project. Just make an editor and throw in some variables. Maybe even cut down on the amount of variables you have all together and be a cool cat like that; you can store variables in the editor, so you know. That opens up a wide world of possibilities.
Here's a pic of the differences to the slider/non slider variants.
In conclusion.
Editor extensions are pretty sick. You can make your code a whole lot more user friendly for not only other programmers you may have on your team, but designers and artists just picking up unity as well.
Creating a nice end-user experience is easy when you have a pleasant experience in the unity editor; and that's why I like to extend the editor wherever I feel it necessary to do so.
From a business standpoint and a standpoint of 'not being that jerk that has the bad code design and layout in the inspector.
Also, in hindsight, I should have used Vector3 variables instead of three floats.
Creating a nice end-user experience is easy when you have a pleasant experience in the unity editor; and that's why I like to extend the editor wherever I feel it necessary to do so.
From a business standpoint and a standpoint of 'not being that jerk that has the bad code design and layout in the inspector.
Also, in hindsight, I should have used Vector3 variables instead of three floats.