Follow

How to customize Unity script templates

Symptoms
  • When I create a new script, the Unity Editor generates its content. For C# scripts it uses the file name as the class name.
Cause
 
Scripts templates are stored in %EDITOR_PATH%\Data\Resources\ScriptTemplates.
 
Resolution

When you are creating a new script, the Unity Editor generates its content. For C# scripts it uses the file name as the class name. Please see the example below:

using UnityEngine;
using System.Collections;

public class MyCustomScript : MonoBehaviour {

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

If you want to change the initial script, you can modify the script templates stored here:

 

If you would 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 within the script, but remember to leave #SCRIPTNAME# where it is. Without it, your template class name will not change and a new script file will be generated with an incorrect class name.

Here is an example how the modified C# template may look: 

/*
 * Modified template by Unity Support.
 */
 
using UnityEngine;
 
public class #SCRIPTNAME# : MonoBehaviour
{
    #region Public Fields
    #endregion
 
    #region Unity Methods
    void Start()
    {
    }
 
    void Update()
    {
    }
    #endregion
 
    #region Private Methods
    #endregion
}
 

After modifying the template files, please relaunch your Unity Editor to apply these changes.

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 again if 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.

More Information
 
 
 
Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

4 Comments

  • 0
    Avatar
    NIKHIL PANDE

    good logic of using templates efficiently unity seems to be self sufficient framework. it could be more useful if theoretical details were provided like utility scope and areas where it is used in the animation graphics and gaming industry[ the sub sectors]

      Nikhil Pande

            Technical Team Lead : Incedo; Chennai, India

  • 0
    Avatar
    tomas almera
    Edited by tomas almera
  • 0
    Avatar
    Tijmen van den Heuvel

    When doing this in version 5.5 i saw something new, in every method body there was the following text: #NOTRIM#

     

    What does this mean?

  • 0
    Avatar
    Iain Gilfeather

    Re. #NOTRIM#, good question.

    The short answer is you can either leave them in your templates or remove them. It makes no difference because they are stripped from any new script asset generated from that template.

    To explain why they are there, in Unity 5.5, tabs were added to some empty lines in the templates for better indentation. The #NOTRIM# text is a special marker used by the template file system to mark lines that end in whitespace, like tabs. They prevent that whitespace from being stripped by our source code tools. The Unity Editor then strips all the markers when the template is used.

Please sign in to leave a comment.