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.

using UnityEngine;
using System.Collections;

public class MyCustomScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

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:

81-C# Script-NewBehaviourScript.cs.txt
82-Javascript-NewBehaviourScript.js.txt
83-Shader__Standard Surface Shader-NewSurfaceShader.shader.txt
84-Shader__Unlit Shader-NewUnlitShader.shader.txt
...

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):

using UnityEngine;
using System.Collections;

public class #SCRIPTNAME# : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

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:

/*
 * Copyright (c) The Knights of Unity
 * http://www.theknightsofunity.com/
 */

using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour
{

    #region Public Fields
    #endregion

    #region Unity Methods

    void Start()
    {
    }

    void Update()
    {
    }

    #endregion

    #region Private Methods
    #endregion
}

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.

related
IntermediateTips
Use decorator drawers to improve inspector view of your scripts
If you're writing a custom script then most probably you want to manipulate its public field...
0
BasicsTips
Use unitypackage files to share part of your project
Did you know that there's an easier way to share your Unity project files than zipping it all...
0
BasicsTips
Use icons to see invisible object on scene
Your game scene may consist of objects that are visible on your scene view (like mesh) and...
0
Call The Knights!
We are here for you.
Please contact us with regards to a Unity project below.



The Knights appreciate your decision!
Expect the first news soon!
hire us!