Intermediate Tips

How to customize Unity script templates

When you’re creating a new script, Unity editor generates its contents and for C# script it uses the file name as the class name. It looks like this.

It’s a good way to start, but if you’re working with Unity for some time then most probably you already have your own style and know how every source file should look like from the beginning. The script template includes using tabs or spaces, line endings characters, namespace, header comments etc. Changing this every time for new scripts to match your style can be a little boring.

What you might not know is that these templates can be changed! You will find template files here:

  • Windows: C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates
  • Mac: /Applications/Unity/Editor/Data/Resources/ScriptTemplates

Go to this directory and you will find several template files:

Of course you don’t have to change all of these files, but only those that you actually want to change. For instance, if you’d like to have a different C# script template, just edit the 81-C# Script-NewBehaviourScript.cs.txt file and leave the rest as it is.

The mentioned file content looks like this (by default):

You can change anything you want, but remember to leave #SCRIPTNAME# where it is. Without it your template class name won’t change and new script file will be generated with a wrong class name.

Here’s an example how the modified C# template may look like:

I’ve added a header comment (all my sources include a copyright entry), removed using System.Collections; because I’m not using collections in every script and I’ve added regions to my class code to improve the navigation (regions can be quickly collapsed or expanded).

After modifying the template files relaunch your Unity editor and enjoy your new script templates!

Be sure to back up your original template files and the modified ones. Original files will be needed if your template is not recognized correctly – you will have to start over when that happens. Make a copy of your modified template somewhere outside the Unity directory. When you upgrade your Unity version, the template files will be overwritten and you will need to copy and replace these again with your custom templates.