Recently, I shared my new tool: Automatic Rule Tile Generation in Unity. Today, I want to share how I actually made it. Tool development in Unity is incredibly useful. You can use it to eliminate some of those repetitive tasks you have to do.
What is Unity Tool Development?
In my case, I am building a game and have a repetitive task: creating rule tiles for all my various tilemaps. All together, I plan to have 8-10 worlds each with 3-5 tilemaps. Plus, I would like some random variants for each tilemap. 24 tilemaps minimum, but possibly up to 200 factoring in the variants. And if you’ve ever done rule tiles in Unity, you know doing that for 200 tilemaps would be a nightmare.
Wouldn’t it be awesome to have a custom tool automate this process? That is where Unity tool development comes into play.
By creating custom editor scripts, you extend the functionality of the Unity editor itself. These scripts interact behind the scenes, allowing you to build custom inspectors (those windows that pop up when you select an object) and functionalities that fit your needs.
Getting Started in Unity Tool Development
Custom Unity tool development requires two key pieces: a scriptable object and the GUI. The scriptable object is the primary functionality of the tool. The GUI is the custom inspector window where the user can configure everything.
Here’s a quick guide:
1.) Create a new Scriptable Object: Within Unity, navigate to Create > C# Script. Give your script a descriptive name that reflects its purpose (e.g., “AutoLevelGenerator” or “PowerUpRandomizer”). I recommend including “SO” or similar to indicate it is the scriptable object. Note, it also needs to reference the : ScriptableObject
base class.
using UnityEngine;
#if UNITY_EDITOR
public class Autotiler_SO : ScriptableObject
{
}
#endif
2.) Create a new GUI Object: This informs Unity that your tool has a custom editor when using a specific class. Add the line [CustomEditor(typeof(YourClassName))]
at the top of your script, replacing YourClassName
with the actual class your tool will edit. In this case, we need the : Editor
base class as well as the UnityEditor
namespace.
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Autotiler_SO))]
public class AutotilerGUI : Editor
}
Developing the Custom User Interface
Personally, I like to start with the GUI first. There are two key pieces.
First, we need to create a bridge between the GUI and the scriptable object itself. That way we can pass information and assets between them. This is done in the OnEnable() method.
private void OnEnable()
{
tilemaps = serializedObject.FindProperty("tilemaps");
ruleTileTemplate = serializedObject.FindProperty("ruleTileTemplate");
autotilerScript = (Autotiler_SO)target;
serializedObject.ApplyModifiedProperties();
}
Here, I’m using serializedObject.FindProperty()
to connect to two properties: my tilemaps and my rule tile template.
Second, we need to actually add the UI elements. This is done in the OnInspectorGUI() method any time the inspector window is refreshed.
public override void OnInspectorGUI()
{
serializedObject.Update();
DrawHeader();
DrawTilemapSection();
DrawRuleTileTemplateSection();
DrawGenerateButton();
serializedObject.ApplyModifiedProperties();
}
private void DrawGenerateButton()
{
if (GUILayout.Button("Generate Rule Tile"))
{
autotilerScript.CreateRuleTiles();
}
}
serializedObject.Update()
makes sure the GUI has the latest information from the SO properties before render the inspector. The DrawX()
methods are where the UI elements are actually placed. Personally, I put each in their own isolated method. (Only DrawGenerateButton()
is shown here.) Finally, serializedObject.ApplyModifiedProperties();
passes any changes back to the SO.
For a full list of what GUI elements are available, be sure to check out the Unity docs on the EditorGUI: https://docs.unity3d.com/ScriptReference/EditorGUI.html.
In my case, the Generate button is where it ties the functionality back to the Scriptable Object. It calls the CreateRuleTiles()
method.
Creating Scriptable Object Functionality
Obviously, the functionality you add will be dependent on the tool you are creating. The main thing to note is you need some way of linking the GUI to the SO. In my example, that was the CreateRuleTile() method on the button. So we need to add that method.
public void CreateRuleTiles()
{
if (IsValidRuleTemplate() && AreTilemapsValid())
{
CopyRuleTileTemplate();
if (tilemaps.Count == 1)
{
ApplySingleTilemap();
}
else
{
ApplyRandomTilemaps();
}
CreateNewRuleTileAsset();
}
}
private bool IsValidRuleTemplate(){}
private bool AreTilemapsValid(){}
private void CopyRuleTileTemplate(){}
private void ApplySingleTilemap(){}
private void ApplyRandomTilemaps(){}
private void CreateNewRuleTileAsset(){}
Here, I’ve added in the CreateRuleTiles()
method that is called from the button. In addition, all the methods are separate to keep everything nice and clean.
Conclusion
Unity tool development allows you to streamline your workflow and create game dev tools tailored to your specific needs. With a basic understanding of C# and the Unity editor, you can build tools that save you time and effort. So, what are you waiting for? Unleash your inner game developer and start exploring the exciting world of Unity tool development!
I’m always interested to learn more about what other dev are creating. Feel free to share your experiences or any questions you have about Unity tool development in the comments below. I’ll do my best to answer them.
The full code used in this example can be found in my repository here: https://github.com/MitchellOpitz/Automatic-Rule-Tile-Generator.