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?
4 out of 4 found this helpful
Have more questions? Submit a request

9 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.

  • 1
    Avatar
    Laurie Harper

    Nice to know. Are there other placeholders available besides #SCRIPTNAME# and #NOTRIM#?

  • 0
    Avatar
    Gregg Tavares

    I sure would be nice if Unity would instead use some user data folder like C:\Users\Person\AppData\Unity\templates or ~/Users/Person/Library/Applicaftion Data/Unity/Templates

    As it is every time a new version of unity is installed I have to manually fix the templates

  • 0
    Avatar
    Thamas Arron Bell

    I Cover this a little further on my website, but the link at the bottom will bring you back here.. aka never ending loop... But I explain how to modify these and not lose them on an install. Best way i have found in short, is to change the number in the beginning or change the name just slightly so Unity doesn't overwrite your template on an install.

    If the file on the install has the same name as the one in the file it is suppose to go in, it will automatically overwrite without even telling you, or asking you. So to prevent, never use original script names. and since we can only use 1 C#  template at a time, the lowest number before your script suffix and name will be the one to load into Unity.

    EG on name Layout:

    # 81 or lower + "-C# Script-" + "ScriptName.cs".txt

     

    Hope that solves your issues @Gregg Tavares

    Edited by Thamas Arron Bell
  • 0
    Avatar
    Liam Rüdel

    While having local templates is great its not very practical when working in a team, but thanks to this tweet: https://twitter.com/UnityBerserkers/status/1105555535070416896 I've come across the solution! You can add project ScriptTemplates. Simply add your templates to Assets/ScriptTemplates and restart the editor.

    You can now create and share( via source control ) script templates on a project level!

  • 0
    Avatar
    Thamas Arron Bell
     
    @Liam Rüdel Great find, should have known better thinkin of Resources and Editor as oither's that work similarly ahah that's awesome though!
Please sign in to leave a comment.