Unity Script Templates
Unity's Default Script Tempalte
Unity provides default script templates that streamline the creation of new scripts by including some pre-defined code and placeholders. This is especially useful if you often use similar structures for your scripts, such as a default MonoBehaviour
class with custom logic.
Unity's deafulat scirpt templates are located in Unity’s installation directory, typically under Editor > Data > Resources > ScriptTemplates
.
Here is an example of a default C# script template provided by Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#NOTRIM#
}
// Update is called once per frame
void Update()
{
#NOTRIM#
}
}
#ROOTNAMESPACEEND#
Script Template Keywords
The default script template includes several keywords that are replaced or managed by Unity during script creation:
-
#SCRIPTNAME#
: This placeholder is replaced with the name of the new script file. It ensures that the class name within the script matches the file name, which is essential for proper functionality. -
#NOTRIM#
: This keyword prevents Unity from removing any trailing whitespace in the template. It ensures that formatting and spacing are preserved as specified in the template. -
#ROOTNAMESPACEBEGIN#
and#ROOTNAMESPACEEND#
: These keywords are used to manage the namespace structure in Unity projects. They mark the beginning and end of the namespace, helping to organize scripts into the correct namespace based on the project's settings.
Custom Script Templates
Custom script tempaltes can streamline your workflow and ensure consistency across your project. While custom tempales can be saved locally in the same director of the Unity editor default templates, it is not prefered as modifying these can potentially affect other projects or be overridden when updating Unity. Instead, consider create a custom script template directly in the project’s Assets
folder. This will ensure that custom script tempales are preserved across different Unity versions and won’t interfere with the editor files.
Tutorial: Details
📝Foundational Skills | 🕒10 minutes | 📂Required File: NA
In this tutorial create script templates.
-
Locate or Create the Script Templates Folder in Your Project:
- In your Unity project, navigate to the
Assets
folder. - Create a new folder named
ScriptTemplates
within theAssets
folder if it doesn’t already exist. This is where we will store our custom script templates.
- In your Unity project, navigate to the
-
Create a New Template:
- Open your
ScriptTemplates
folder. - Copy an existing template, like
81-C# Script-NewBehaviourScript.cs.txt
(you can find default templates in Unity’s installation folder underEditor > Data > Resources > ScriptTemplates
if you need a reference). - Paste it into the
ScriptTemplates
folder in your project, and rename the file.- Name the file
81-CSG Templates__NewBehaviourScript.cs.txt
- Name the file
- Open your
-
Edit the Template:
- Open the newly created template file in any text editor.
- Modify the content of the file to include your custom code.
Naming Script Templates
Script templates should start with a unique number (e.g., 82-MyCustomScript.cs.txt
). When naming your template, you can also create a submenu in Unity's Create menu to better organize your templates. This submenu is a contextual grouping that appears when you go to Create a new game asset, rather than a physical folder in your project.
To create a submenu, include the submenu name after the unique number and hyphen, followed by double underscores (__
), before the template file name. For example, 81-CSG Templates__NewBehaviourScript.cs.txt
creates a CSG Templates submenu, and your script template will appear under that submenu in the Create menu.
Sub-Submenus
If you want to create additional levels of organization, you can add sub-submenus. Simply extend the naming pattern by adding another submenu name, followed by double underscores. For example, 91-CSG Templates__Advanced Scripts__NewScriptableObject.cs.txt
would create a CSG Templates submenu, and within that, a sub-submenu called Advanced Scripts, where your NewScriptableObject template will be listed.
Example:
81-CSG Templates__NewBehaviourScript.cs.txt
- CSG Templates submenu in the Create menu.
91-CSG Templates__Advanced Scripts__NewScriptableObject.cs.txt
- CSG Templates submenu → Advanced Scripts sub-submenu.
By using this pattern, you can keep your script templates well-organized within Unity's Create menu, without altering the physical folder structure of your project.
Documentation Coments
When creating a custom script template for Unity, it's good practice to include documentation comments at the top of the script. These comments provide essential information about the script, including its purpose, the project it’s associated with, and revision history. This practice is useful for maintaining clarity, even in solo projects, especially when revisiting scripts that haven't been touched for a while.
The documentation comments should follow this format:
/*******************************************************************
* COPYRIGHT : Year
* PROJECT : Name of Project or Assignment script is used for.
* FILE NAME : ScriptName.cs
* DESCRIPTION : Short Description of script.
*
* REVISION HISTORY:
* Date [YYYY/MM/DD] | Author | Comments
* ---------------------------------------------------------------------------
* 2000/01/01 | Developer's Name | Created <short comment of changes>
*
*
/******************************************************************/
Adding the Awake Method
In addition to documentation comments, we will include the Awake method in our script template. The Awake method is called before the Start method when a game object is instantiated, making it useful for initialization tasks that need to occur before other components are active.
Final Script Template
Combining the documentation comments with the Awake method, our final script template will look like this:
/*******************************************************************
* COPYRIGHT : Year
* PROJECT : Name of Project or Assignment script is used for.
* FILE NAME : #SCRIPTNAME#.cs
* DESCRIPTION : Short Description of script.
*
* REVISION HISTORY:
* Date Author Comments
* ---------------------------------------------------------------------------
* 2000/01/01 Developer's Name Created <short comment of changes>
*
*
/******************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
// Awake is called once at instantiation
void Awake()
{
#NOTRIM#
}//end Awake()
// Start is called before the first frame update
void Start()
{
#NOTRIM#
}//end Start()
// Update is called once per frame
void Update()
{
#NOTRIM#
}//end Update()
}
#ROOTNAMESPACEEND#