Unity Script Templates

It’s feature that almost every Unity user used but not everyone realize it’s full potential.
So what exactly we are talking about?
The “Create Asset Menu” that can be opened in few different ways like:

  • Top bar “Assets” menu
  • Project window -> “Create” button
  • Project window -> right mouse button and by selecting “Create”

Almost every time we want to add some logic to our game we open “Create Asset Menu” and click on “C# Script” that looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour {

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

And while it’s your first steps with Unity that comment lines with explanation what is Start & Update might be nice? When you’re already in for months or years you can be already really bored of deleting them…

But it’s really easy to remove that comments and do many more things, like: modifying any current template, adding own templates eg. scriptable object or custom inspector, adding sub category for templates.

Let’s go one by one:


To modify any current template you need to find Unity templates directory in internal Resources.  It’s in %EDITOR_PATH%\Data\Resources\ScriptTemplates.
Depending on platform it could be:

  • PC: “C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates” (or similar)
  • MAC: /Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates/ (or similar)
  • Linux: /opt/Unity/Editor/Data/Resources/ScriptTemplates

Next find script you’re interested in, most probably it’s “81-C# Script-NewBehaviourScript.cs.txt”, open and modify it (you might want to backup this directory before modification). And you wont need to remove that annoying comments ever again! Fuck yea.


Adding own templates is also really easy. The easiest way is to just duplicate one of existing scripts and change that part between hyphens like “C# Script” to something else and restart Unity.

But to be efficient with creating own templates there are few things that it’s worth to know like filename format and tags.

The filename format is:

{SortingIndex}{MenuItemName}{FileName}.txt

{SortingIndex} is some number used to order the items in the “Create New” context menu, duplicates are allowed.

{MenuItemName} is a string that defines the name of the item. It can be a nested item, use double underscores “__” to indicate going into a folder, sorta like with the [CreateAssetMenu] attribute, except in that one they use forward slash “/” instead. A bit confusing.

{FileName} is the default suggested filename that Unity has highlighted, when the user clicks the menu item to create the file. There seems to be some additional magic string searches going on for the filename. If the string “Test” appears in the filename (case insensitive), Unity will automatically create and move the new file to an Editor folder, in the current directory.

Tags as well as some of my custom templates You can find on my GitHub unity templates repository here:

https://github.com/karolwieczorek/Custom-Unity-script-templates/tree/develop

 

You can also look here for some official info:

https://support.unity3d.com/hc/en-us/articles/210223733-How-to-customize-Unity-script-templates

Leave a Reply

Your email address will not be published. Required fields are marked *