Script templates and base classes
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.
Older Posts
- Eventual Exceptions vs Programming in a Minimal Functional Style
- The Mystery of Flunf
- Explain it like I’m Five: The Socialist Millionaire Problem and Secure Multi-Party Computation
- Computer Science Blows My Mind
- A visit to Execution Labs in Montréal
- Transmuting Dice, Conserving Entropy
- Rule of Thumb: Ask for the Clock
- Rule of Thumb: Use Purposefully Weakened Methods
- Rule of thumb: Preconditions Should be Checked Explicitly
- Intersecting Linked Lists Faster
- Mouse Path Smoothing for Jack Lumber
- My Bug, My Bad #2: Sunk by Float
- Repeat Yourself Differently
- Grover’s Quantum Search Algorithm
- Followup to Non-Nullable Types vs C#
- Optimizing Just in Time with Expression Trees
- When One-Way Latency Doesn’t Matter
- Determining exactly if/when/where a moving line intersected a moving point
- Emulating Actors in C# with Async/Await
- Making an immutable queue with guaranteed constant time operations
- Improving Checked Exceptions
- Perishable Collections: The Benefits of Removal-by-Lifetime
- Decoupling shared control
- Decoupling inlined UI code
- Linq to Collections: Beyond IEnumerable<T>
- Publish your .Net library as a NuGet package
- When null is not enough: an option type for C#
- Unfathomable Bugs #5: Readonly or not
- Minkowski sums: examples
- My Bug, My Bad #1: Fractal Spheres
- Working around the brittle UI Virtualization in Windows 8
- Encapsulating Angles
- Unfathomable Bugs #4: Keys that aren’t
- How would I even use a monad (in C#)?
- Useful/Interesting Methods #1: Observable.WhenEach
- Unfathomable Bugs #3: Stringing you along
- Anonymous Implementation Classes – A Design Pattern for C#
- Tasks for ActionScript 3 – Improving on Event-Driven Programming
- Minkowski sums and differences
- Non-Nullable Types vs C#: Fixing the Billion Dollar Mistake
- Unfathomable Bugs #2: Slashing Out
- Unity font extraction
- Abusing “Phantom Types” to Encode List Lengths Into Their Type
- Constructive Criticism of the Reactive Extensions API
- Quaternions part 3
- Quaternions part 2
- Quaternions part 1
- Unfathomable Bugs #1: You can have things! You can have things IN things! You can have …
- Coroutines – More than you want to know
- Asset Bundle Helper
- The Visual Studio goes away
- .Net’s time traveling StopWatch
- Polish
- Introducing Catalyst
