I’ve published my first asset on the Unity Asset Store! Find it for free here: https://assetstore.unity.com/packages/tools/utilities/fence-layout-tool-162856
Or get the code directly from GitHub: https://github.com/AndyFenwick/FenceLayout
[EDIT: If anyone is interested, it’s had 142 downloads in the first five weeks after release. 29 in the first three days but it’s steadily getting a few each day since. That’s actually more than I was expecting.]
Fences
While making the Frozen game I wanted to put some fences down and assumed there would be a simple script on the asset store for this. There were a few fence tools available but they were all paid for. I’ve bought a bunch of art assets and tools (modelling definitely isn’t my strong point), but writing a simple tool to put a few objects in a line seemed like a perfect learning opportunity!
Here’s the tutorial video if you want to see what it does:
Writing the actual layout script was mostly simple. Add the prefabs, set up spacing, scaling and offsets, sample the terrain height and plop the prefabs down between the waypoints.
The only non-trivial part was dealing with uneven ground. On mostly flat ground the fences all line up nicely and look good. However on sloped terrain their disjointed and partially underground. The solution is to shear the fence model in Y so that both ends are on the ground. The only problem here is that Unity transforms don’t directly support shear. However there is a workaround by using two parent transforms and a series of rotations and non-linear scales. See this: https://answers.unity.com/questions/961330/shear-transformation-using-gameobject-transformati.html
I implemented that and it works well, with nice curving fences:

I’m not sure what happens to the colliders – the debug rendering becomes detached somewhat from the geometry, but it’s close enough that it works in my game so I’ve not investigated further.
Custom editor
The most fiddly bit was getting the custom editor and inspector working nicely in Unity. The input handling / selection handling stuff is pretty confusing – controls either wouldn’t consume input events, or they’d consume too many events and it would be impossible to select anything else. After some experimentation, this setup seems to work:
void InputUpdate(SceneView sceneview)
{
Event e = Event.current;
int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
if (e.control)
{
HandleUtility.AddDefaultControl(controlID);
}
if (e.type != EventType.Repaint && e.type != EventType.Layout)
{
GameObject fenceObj = Selection.activeObject as GameObject;
if (fenceObj)
{
FenceLayout fence = fenceObj.GetComponent<FenceLayout>();
// Do things with the fence...
}
}
}
public void OnEnable()
{
SceneView.duringSceneGui += InputUpdate;
}
public void OnDisable()
{
SceneView.duringSceneGui -= InputUpdate;
}
I also had various bugs with things not saving until I found out you have to manually set the object as dirty:
EditorUtility.SetDirty(m_fenceLayout);
And then found that editing a prefab still wasn’t saving. The fix for that was to mark the whole scene dirty if anything has changed:
if (EditorGUI.EndChangeCheck())
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
Have a look at the full code on GitHub to see the specifics.
Admin
I hadn’t quite realised the amount of admin that’s required to get something on the asset store. We publish enough apps in my day-job, but when you just want to publish a simple script it’s a bit of work to make (and work out what to put on) the required thumbnails, screenshots, social media banners etc. Plus write the documentation, record a tutorial video, write descriptions and all that. Good learning experience though.
Thanks for making this available it was just what I was looking for and super easy to use too!
Screenshot of my example: https://twitter.com/rwbrowne/status/1280235246789820418