Your Rules, Your Tools
We use Unity to make Shadow Bug. Unity has a pretty neat editor, and you can make it even neater by making some tools of your own. So if you use Unity but haven’t gotten around to editor scripting, this post is for especially for you! I’m going to present some of the tools I’ve made, and I hope they inspire you to make tools too ?
Tools tools tools! ? ? ⚒ ⛏
I personally started creating custom tools in Unity way too late. If I had known how easy it is, I would have been spared from a lot of manual labor in some of my previous projects. So when it comes to Shadow Bug, I make all kinds of random tools without hesitation. A lot of them I don’t even end up using too often, but they still always (well, almost always ?) save me time. And of course there are tools that I keep using often. Like the Data Helper window you can see in the screenshot above, that I use to unlock specific levels in our map without actually having to play them all the time.
My tool for moving our parallax background.
Above is a tool that turned into a nice GIF We have a parallax background in the game with quite a lot of layers, and setting it up takes some time in each level. So Juha had a problem, when he wanted to change the starting position of Shadow Bug in one of the levels, but doing that would mess up the parallax background positioning. This is because if he moves the player object in the editor, the background doesn’t follow properly. So he asked me to make a tool for this, and I did! Now he just has to have the tool’s window open when moving Shadow Bug, and the background and all its layers move along nicely, using the same logic as in the game. Another satisfied customer ?
A tool for giving our ground pieces some bling ?
One of the tools I made is used to patch the ground pieces in our game. The ground piece prefabs don’t have edge pieces on them, since we want to scale them to make them longer, but we don’t want to scale the edge pieces. So I made a tool that adds the edge pieces later. It can also place props like grass on the ground with given parameters like density. This tool is an example of a Custom Editor, unlike my parallax tool, which is an Editor Window. An Editor Window is what you might expect: it’s an independent window with whatever functionality you have given it. Custom Editors however extend a script that you have created. So for instance the buttons you see in the GIF above are created by making a Custom Editor for the Ground Patcher script. The Custom Editor script also contains the logic for pushing those buttons.
There’s one big reason why I wanted to make the Ground Patcher tool as a Custom Editor instead of an Editor Window tool: saving data. In Unity it’s super fast to make slots for variables in the editor – just declare them public. And what I mean by ‘slots’, you can see in the Ground Patcher GIF above: slots where you can place different prefabs for props, or a float value for density and so on. So Unity allows you to create the user interface for your tool fast and it also allows you to save data without making any logic for that yourself. You can just add a script that has a Custom Editor made for it into a Game Object and then save that object as a prefab. Now you have a custom tool with the properties you wanted saved. And you can make different prefabs of the same tool! For instance I could have the prefab of the Ground Patcher that is shown above to be used in forest levels. And then I could make another prefab to be used in sewer levels with different props and density. Neat and easy! And if all these Custom Editors and Editor Windows sound scary, don’t worry. They are super easy to make! The Unity documentation has excellent examples for Editor Windows and Custom Editors.
Up there you can see a tool for setting up what’s needed for making our enemies die like champs (in Shadow Bug, enemies split to pieces when they die). Our monsters are made up of many sprites, and we use that to our advantage in their death animation. What my tool does is, it searches for all the Sprite Renderers in a monster, and saves them in a list. It also gives all the objects in the list the necessary components needed: rigidbodies, colliders and a self-made Body Part script. After this automation, we can manually make adjustments to some of the body parts, since the components are in place. For instance, our Body Part script has a few checkboxes that allow some body parts to behave differently than others. Like the eyes of the monsters, we want them to fade away really fast, because they looked really ugly when they behaved like other body parts ?
WTF is this???
And finally: like I said, I don’t hesitate when it comes to making a new tool. Sometimes this leads to pretty random tools, like the one above: Circular Copy. It makes copies of the selected object and places them on a given radius from the original object while rotating the copies appropriately. I’ve completely forgotten what I made it for! At least it makes a pretty pattern out of monsters. So I guess somewhere in some level of Shadow Bug, there is something that has required Circular Copying. If you ever find it, let me know ?