SILO27: CRASHLANDED Nice Dice Roller: friendly & customizable design OTHER BLOG POSTS

How to make custom script templates in Unity

Cover image

You’ve probably noticed that whenever you create a script in Unity, a simple MonoBehaviour script is already written in it. It even includes the filename you entered as the class’ name. But did you know that, even if there’s no trace of it in the editor, these templates are customizable? You can change them and even make new ones, like for inspectors, scriptable-objects or editor windows!

And it’s not too difficult either!

How to customize

The built-in templates are inside your editor folder, wherever you installed it. For example, on Windows that is %EDITOR_PATH%\Data\Resources\ScriptTemplates.

You can directly edit these built-in ones, but keep in mind that future Unity updates will overwrite them.

A better solution is to put your custom tempates directly inside your project’s Asset folder at Assets/ScriptTemplates. This way can also check them into version control, and other people on your team will be able to use them too.

Unity reads templates only on startup, so you’ll need to restart the editor everytime you make a change!

File name syntax

This is the trickier part. How The file name is formatted changes how they show up in the “create”-menu.

It begins with a number that determines where in the menu our file will show up. For example (at the time of writing) Unity’s default script has a number of 81. Which means that if you use a lower number your entry will be before, and a larger number will be after.

Then, after a single dash, is a list of entries. Each entry will represent what shows up in the menus, and they are each separated by two underscores. The last one of this list will be the title that you’ll click to create your script or file.

Screenshot of right-click menu with custom script template option

The following portion is again separated by a dash. This is the file’s default name, which Unity prefills in the new-file input box.

The extension you want to use for your generated file (.cs in this case for a C# script) comes next.
And finally the .txt is the actual template file extension.

Automatic script naming

As previously mentioned, Unity generates the class name of the MonoBehaviour scripts with the filename that you entered. You can do the same in your templates too! Every time you write ‘#SCRIPTNAME#’ in your template, Unity will automatically replace it with the file name.

Automatic namespaces

This was added in Unity 2020.2.0f1

If you specify a “root namespace” on an AssemblyDefinition, you can use #ROOTNAMESPACEBEGIN# inside a template and Unity will generate the script accordingly. Nothing will break if you don’t specify anything; the placeholder will simply be skipped.

You can also set a root namespace for your whole project in Project Settings > Editor > Project Root namespace.

Important: you also need to put #ROOTNAMESPACEEND# where you want to close the namespace tag, usually this is just at the end of the file.

In the image above, you can also see #NOTRIM#, which tells Unity to keep the whitespace on those lines where it is written, which would otherwise get removed.

A step further: create by script

Since Unity 2019.1 it also possible to create files from templates by code.
In earlier Unity versions this method was internal and only accessible through reflection.

UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, defaultNewFileName);


Sources and more things to read.

Download files

The Patreon files include some example templates and an editor script that lets you create any script from your templates. By using the method mentioned in the tutorial, which reads the file directly from disk when called, it removes the need to relaunch the Unity editor everytime you edit a template.

Enjoying the tutorials? Are they useful? Want more?

If you think these posts have either helped or inspired you, please consider supporting this blog.

Help this blog continue existing

If the knowledge you have gained had a significant impact on your project, a mention in the credits would be very appreciated.

Silo27 Logo
Enrico Monese

All around developer, from games and web to weird commandline utilities. 2D/3D art, with a good care for UI/UX and clean design.

Twitter Twitter Patreon Patreon Mastodon Mastodon Blog Blog   Reddit Reddit Github Github Gitlab Gitlab ArtStation ArtStation Instagram Instagram Mail Mail