Custom Unity & VS Script Templates

One of the first things I always do when I start working with Unity and Visual Studio on a new development machine is setup the development environment just the way I like it. One of the things this involves is modifying the default code templates for creating a new script in Unity or a new class in Visual Studio.

Apart from saving me time having to write boiler plate code or having a new file be just the way I like it, there can also be genuine performance benefits to altering Unity's default templates! But more about that later, first lets look at changing the templates.

Unity Templates

Unity's default templates can be found under your Unity installation's directory in Editor\Data\Resources\ScriptTemplates for Windows and /Contents/Resources/ScriptTemplates for OSX.

The file we are interested in is named something similar to C# Script-NewBehaviourScript.cs.txt and will contain text similar to below:

using UnityEngine;
using System.Collections;

public class #SCRIPTNAME# : MonoBehaviour {

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

#SCRIPTNAME# refers to the name of the class used when creating the script in Unity, apart from that one variable (with exceptions) you are free to modify the rest of the file as you please. I tend to drop the starting { on its own line and remove the Start and Update functions as well as the using System.Collections; from the file, resulting in:

using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour
{

}

then just save this, restart Unity and try creating a new script, and presto! Your own custom Unity template.

So what were those performance benefits I mentioned? Well they only really apply if you have LOTS of scripts, but due to the way Unity's messaging system works (http://blogs.unity3d.com/2015/12/23/1k-update-calls/) having a lot of empty Update functions in scripts can introduce a lot of overhead! And many people forget or can't be bothered deleting the empty Update if they don't use it. So save yourself some hassle optimising later down the line and just get rid of what you don't always need from the start, if you need the Update just add it when necessary.

Note: you may have to do this each time you update Unity to a new version

Visual Studio Templates

Similar to Unity's templates we just need to find the template VS is using and modify it to be what we prefer! They are found under your VS install directory in Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class and the file is Class.cs and will contain text like below:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

$rootnamespace$ is the default namespace of your project and $safeitemrootname$ is generally the name you gave to the file when you created it. Modify this to your liking and then save and restart Visual Studio for the changes to take effect. I normally remove all the using references and the namespace and have it just create a public class.

IndyBonez

Read more posts by this author.