Custom Script Templates

I hate busywork. Whenever I add a new script to a Unity project, it comes with some requisite amount of whitespace cleanup, re-commenting, and other housekeeping I need to do just to get ready to start writing code. Luckily, that boilerplate code isn’t baked into Unity, it comes from a template that, get this, you can replace.

These files are easy enough to locate, with a little bit of digging. In Windows, they’re right alongside the Unity installation:

C:\Program Files (x86)\Unity\Editor\Data\Resources\ScriptTemplates

On Mac, it’s a little trickier. You’ve got to navigate to the Unity package in Finder, right-click and “show package contents,” then navigate to:

Contents/Resources/ScriptTemplates

script templates

And there they are! Feel free to open them up in a text editor, edit to your liking, and save (you’ll have to provide admin rights on Windows). For example, here’s my customized C# NewBehaviourScript:

// --------------------------------
// <copyright file="#SCRIPTNAME#.cs" company="Rumor Games">
//     Copyright (C) Rumor Games, LLC.  All rights reserved.
// </copyright>
// --------------------------------

using System.Collections;
using UnityEngine;

/// <summary>
/// #SCRIPTNAME# class.
/// </summary>
public class #SCRIPTNAME# : MonoBehaviour
{
    /// <summary>
    /// Initialize script state.
    /// </summary>
    internal void Start()
    {

    }

    /// <summary>
    /// Update script, called once per frame.
    /// </summary>
    internal void Update()
    {

    }
}

You can even add entirely new templates.

script templates extended

For example, this template generates a new class with a parameterless constructor:

// --------------------------------
// <copyright file="#SCRIPTNAME#.cs" company="Rumor Games">
//     Copyright (C) Rumor Games, LLC.  All rights reserved.
// </copyright>
// --------------------------------

using System.Collections;
using UnityEngine;

/// <summary>
/// #SCRIPTNAME# class.
/// </summary>
public class #SCRIPTNAME#
{
	/// <summary>
	/// Initializes a new instance of the #SCRIPTNAME# class.
	/// </summary>
	public #SCRIPTNAME#()
	{
	}
}

Unfortunately, since these files fall under the domain of Unity-installed files and not user files, these changes get stomped on every time you update Unity. It’s easy enough to set up a script to replace them, though, or at least to back up your custom templates to your cloud of choice and copy them in whenever you update.

Bookmark the permalink.

Comments are closed