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:
Script 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 initializationvoid Start () { } // Update is called once per framevoid Update () { }
If you want to change the initial script, you can modify the script templates stored here:
- Windows: C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates
- Mac: /Applications/Unity/Editor/Data/Resources/ScriptTemplates
- Mac (since 5.2.1f1): /Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates
In this directory 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 ...
If you want a different C# script template, edit the 81-C# Script-NewBehaviourScript.cs.txt file and leave the rest.
The mentioned file content looks like this (by default):
using UnityEngine; using System.Collections; public class #SCRIPTNAME# : MonoBehaviour { // Use this for initializationvoid Start () { } // Update is called once per framevoid 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 Methodsvoid Start() { } void Update() { } #endregion#region Private Methods#endregion }
After modifying the template files, please relaunch the Unity Editor to apply these changes.
Be sure to back up your original template files and the modified ones. You will need original files if your template is not recognized correctly. If your template isn't recognized, you will have to start again.
Be sure to 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.
Note: you can allocate your scripting templates on a single project by creating a ScriptTemplates folder on the Assets and following the previous steps.
More Information: