My Unity C# Script Template

Updated 8/8/2015 – Added custom keywords to the template, to see how they work please refer to this post.

When you create a new script in Unity3D you end up with a rather basic pre-generated class. As you may know you can change that and I’ve been happy with the template I created for the last couple of years. It contains a self-instantiating singleton as I use singletons in all my managers, some people think they are evil for coupling and other various reasons as pointed out here but I’ve been happy with them at least in Unity scripting so far.

It also contains regions for different kinds of class members, and some people hate regions as pointed out here and many other places. As any other tool if you are using regions to hide dirty/ugly code it’s understandable to accept regions as a “bad programming habit”. But the IDE I’m using supports them and I find the ability to hide the members I don’t need -while I’m just adding a new method for example- convenient.

You can also see the directive “#SCRIPTNAME# here and there. Unity asset preprocessor just replaces them with the script filename so that’s a nice touch there too.

To change your default template just change the file located in the below locations according to the OS that you use

Windows

…\Unity\Editor\Data\Resources\ScriptTemplates

OSX

Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates

Just open it in your favorite text editor or IDE and edit it to your liking. You can use my template given below without asking for permission.

 

Leave a Reply

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