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: 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: so, it should be pretty easy to add some extra using declarations and get rid of those pesky default methods: 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: and then you can define a template like this: 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.
Script templates and base classes
Script Templates
using UnityEngine;
using System.Collections;
public class #SCRIPTNAME# : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
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 {
}
//filename = "ScriptBase.cs"
using UnityEngine;
public partial class ScriptBase : MonoBehaviour {
}
//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#>(); }
}
}
3 Responses to “Script templates and base classes”
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
You said you can add new ones. I added one (85) and got it to look the way that I wanted but I am unsure of how to implement it into Unity. I don’t want to edit much of the original NewC# script, I just want to add a new type of script with its own look. Do you know how to get unity to recognize that there is a new scriptTemplate to use?
For anyone else that stumbles here, all you need to do is make sure your file format naming convention is correct and then close and re-open Unity.
Thanks so much! I didn’t know you could add new templates and that makes life so much more awesome
The number at the beginning is for ordering purposes in the Create menu. Unity creates one of those divider things every 50, which is why folder is above C# Script with a divider.