Script templates and base classes

posted by harrison on September 15, 2012

What’s are the first things you do when you create a new script in unity?

For me, it’s pretty much always: delete the start and update methods, and include System.Collections.Generic and System.Linq.

There’s a solution for that:

Script Templates

if you go to the folder where you installed unity, and find “Editor/Data/Resources/ScriptTemplates/”, you should find several text files there with funny names of the form “number-type-defaultName.ext.txt”. these are the script templates, and you can modify them or add more of them.

I’m not sure what the number is for, but the ‘type’ part is what it will show up as in the “Assets/Create” menu in unity, and the ‘defaultName.ext’ part is what the default filename will be.

 

and if you open up one of those templates, it should look like this:


using UnityEngine;
using System.Collections;
 

public class #SCRIPTNAME# : MonoBehaviour {

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {

	}
}

 

so, it should be pretty easy to add some extra using declarations and get rid of those pesky default methods:


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using Rand = UnityEngine.Random;
using Obj = UnityEngine.Object;

public class #SCRIPTNAME# : MonoBehaviour {

}

I’m using Rand = UnityEngine.Random because there’s a namespace conflict with UnityEngine and System, and this is a pretty easy way to resolve it. Whenever you would use Random, use Rand instead. same for Object.

We can go further than this. Have you noticed that there are properties for all of unity’s components, but for your own, you have to call GetComponent? we can use templates, and a partial class as a base class to get the same thing for your own components. In your project, you will need a base class script:


//filename = "ScriptBase.cs"
using UnityEngine;
public partial class ScriptBase : MonoBehaviour {

}

and then you can define a template like this:


//filename = "84-C# Component-NewComponentScript.cs.txt"
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using Rand = UnityEngine.Random;
using Obj = UnityEngine.Object;

public class #SCRIPTNAME# : ScriptBase {

}

public partial class ScriptBase {
	public #SCRIPTNAME# #SCRIPTNAME# {
		get { return GetComponent<#SCRIPTNAME#>(); }
	}
}

and now any component that derives from ScriptBase will get access to your component. this works because all instances of #SCRIPTNAME# are replaced with the script’s name, and all of the partial classes of the same name are mashed together when compiling, which is great when you’re generating code. if you wanted to keep it a little cleaner, you could prefix the generated property’s name with something, so that it didn’t clutter up intellisense as much.

The beauty of this method is that you can also use the partial base class to add other functionality to all of your scripts as well. so, if you wanted to use a custom event system or something, you could integrate it better.


Twisted Oak Studios offers consulting and development on high-tech interactive projects. Check out our portfolio, or Give us a shout if you have anything you think some really rad engineers should help you with.

Archive

More interesting posts (21 of 33 articles)

Or check out our Portfolio.