How to make custom script templates in Unity
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
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 sintax
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.
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.
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.
Sources and more things to read.
- Creating Custom Unity Script Templates - WeirdBeard’s Blog
- Unity Support - How to customize Unity script templates
- How to create custom script template in Unity
- How to customize Unity script templates - The Knights of Unity
- Templates for Unity - ZeredaGames
- Examples of Script Templates
- How to create scripts from templates by code - Unity Forum
- How to create scripts from templates by code - Github Gist
- custom replacements like FOLDER_NAME
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.