Creating a GUI based Editor Tool

For my archaeological project I need to create trenches in Unity…a lot of them. After throwing away my attempt to re-use RAM splines I’ve decided to write it from scratch <oh boy>.

The Basic Feature

  1. I should be able to specify some common attributes about different trench types and be able to re-use them

This is the blog of my first attempt at implementing this.

Re-using Trench Types

This sounds like the perfect use case for a Scriptable Object. (BTW, did you know Editor derives from Scriptable Object?) It looks a little like this;

I can then create an asset for a ‘slip trench’

Instance of a Trench Scriptable Object

Represent two points

I need something visual to hang these points off, plus it’s not really two points, I should be able to use 2 or more points, e.g. a zig-zag trench. I also needed something to hang the Scriptable Trench off, so here comes the Trench behaviour

Visualise the Trench

Now it’s gets real. I need to implement an Editor to bring all this together and to provide the GUI for it.

The menu item then shows up in Unity

The static then assigns a new instance of our behaviour via the create trench builder call.

It creates a new game object, assigns the default layer and crucially adds the Trench behaviour.

Currently you still have manually create a couple of empty games to the Trench Markers

Now we have the object graph in the project hierarchy we can visualise it…

OnSceneGUI & Handles

This is where all the fun of the visualiser resides. Since it’s a method in the Editor class we have access to the base ‘target’ property which in this case will return a Trench. I’ll just show the code;

That gives a nice visualiser of the point names and a line between them. But the Handles utility classes have another trick up their sleeves, they can provide Unity tool support too. For example, let’s add the Move tool to each Marker AND respond to the user moving them — remember typically when you select a child you lose selection on the parent, i.e. here we can keep all the children Markers showing their positions whilst the user is moving one of them.

Plus one final little flourish, snap the markers to the Terrain…

Now I have a basic, but working visualizer that snaps the points to the Terrain.

Snap to Terrain

Unity, VR, Enterprise and .Net Developer