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:
Comments
17 comments
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
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?
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.
Nice to know. Are there other placeholders available besides #SCRIPTNAME# and #NOTRIM#?
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
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
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!
If you use the Uniyt Hub, the location of the files is at C:\Program Files\Unity\Hub\Editor\<UNITY_VERSION>\Editor\Data\Resources\ScriptTemplates
super helpful, thanks
I am not sure if this has changed, but I am using version 2019.3.15f1 on Windows 10, thus my ScriptTemplates folder is at "C:\Program Files\Unity\Hub\Editor\2019.3.15f1\Editor\Data\Resources\ScriptTemplates", I have added my custom script template and have named it "82-C# Script-NewKitBehaviourScript.cs.txt", however, it does not show in the Add Component list.
The contents of the cs template I have created match those in the "81-C# Script-NewBehaviourScript.cs.txt". As a basic test of this issue, I changed the name of that file to "81-C# Script-NewBehaviourScriptA.cs.txt", which I imagine should change the name in the editor, however, it does not - it appears to have no effect.
I am not sure what I am not doing correctly.
Well, it looks like changing the name of the original NewBehaviourScript did have an affect on Unity. I get this now when I try to create a new behaviour script:
FileNotFoundException: Could not find file "C:\Program Files\Unity\Hub\Editor\2019.3.15f1\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt"
So, this must mean that path is being stored somewhere. I'd need to change that too - anybody got a clue where it might me?
Make sure to restart the Unity Editor after making any script template changes. I prefer to keep my script templates in my project, but whichever location you use, you still have to restart the editor after changes for them to be seen.
Thanks Laurie, however, I did reset. I really would prefer to keep the templates in my project like you have suggested; how do you handle that, and how do you do it?
@Stacy see this earlier comment.
it is 2021 now, this is the far easier way to do it:
https://forum.unity.com/threads/so-i-tried-to-change-my-script-template-today.1088719/#post-7012651
Please sign in to leave a comment.