Template Creation Basics

Template Creation Basics

To create a brand new template, two things are needed: the Template file, and a Methods file.  The Template file is like a skeleton -- it defines how your generated script should look, and how it should be laid out.  The Methods file is more like the meat of the operation -- it defines what methods are available for selection via the Script Creation Wizard.

First things first, let's look into what goes into creating a Template file.

Creating a Template File

The #1 most important thing about a Template file is its file extension.  It's extremely important that all Template files end with a .cs.txt extension for uTemplate to register them properly.

Once the Template file is created, you can start adding some content to it.  As an example, here's how uTemplate's MonoBehaviour Template looks:

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

public class #SCRIPTNAME# : MonoBehaviour 

Notice that most of the content is generated by uTemplate when a script is created.  You can think of Template files as a kind of blueprint that uTemplate reads to build your final script.  Keeping the Template files content-independant is one of the factors that makes Template files so powerful.

Creating a Methods File

Much like Template files, Methods files need to have a specific file extension.  It's extremely important that all Methods files end with a .methods.txt extension for uTemplate to register them properly.

Once the Methods file is created, you can start adding some content to it.  As an example, here's how uTemplate's ScriptableObject Methods file looks:

[For ScriptableObject]
//This function is called when the scriptable object will be destroyed.
void OnDestroy()
//This function is called when the scriptable object goes out of scope.
void OnDisable()
//This function is called when the object is loaded.
void OnEnable()

As you can see, Methods files are pretty intuitive, much like Template files.  Adding methods to these files is simple -- you're basically creating partial methods (i.e. methods without a body).  Much like full methods, they can have comments preceding them, and any Access Modifiers will also be carried over and automatically selected in the Script Creation Wizard.

Tagging Methods

Methods can also be tagged as a default method, or with a specific Unity version.  When tagged as default, the method will automatically be checked in the Script Creation Wizard, unless it's unchecked manually later.  When tagged with a Unity version tag, the method will only be displayed in the Script Creation Wizard if the current Unity version is supported.  A tagged method would look like this:

//This is a method that's selected by default.
[DEFAULT] void DefaultMethod()

//This is a method that'll only work with Unity 5.3 or greater.
[UNITY >= 5.3] void GreaterOrEqualMethod()

//This is a method that'll only work with Unity 5.3.
[UNITY == 5.3] void EqualToMethod()

//This is a method that'll only work with versions less than Unity 5.3.
[UNITY < 5.3] void LessThanMethod()

Note: when using version comparisons less than (<), less than or equal to (<=), equal to (==), greater than (>) and greater than or equal to (>=) are all valid!  Nice!

Linking Methods to Template Types

The first line of every Methods file should contain what Types these methods apply to.  In this example, we're specifying that these methods are for ScriptableObject.  If you had multiple Types you wanted to mark as applicable, you can add a comma-separated list like so:

[For ScriptableObject, YourSpecialClass]
Methods Inheritance

One of the cooler features of Methods files comes in the form of Inheritance.  In the above example, we specify that the Methods file is for ScriptableObject.  But what about MonoBehaviour, which also has OnDestroy, OnDisable and OnEnable methods?

Because MonoBehaviour derives from ScriptableObject, uTemplate is smart enough to see the connection and automatically apply valid ScriptableObject methods to MonoBehaviour types in the Script Creation Wizard.  This is all automatic, and applies to your custom classes as well.  If you have a class named Child derived from Parent, and you make a Methods file for Parent, your Child Template will recieve the Parent methods "for free" without having to generate a Methods file.


Additional Reference

The built-in Template and Methods files are a great place to start if you're looking for a jumping-off point into creating your own files.  uTemplate fully supports the overriding of its core Template and Methods files, so you could, in fact, copy all of the built-in files into your own directories, modify them, and point uTemplate at the directories via the Preferences.  uTemplate will always try to load user files over core files, so your changes to the core files will not only be prioritized, they won't be overwritten by upgrading uTemplate to a new version.