T4 Code Generation in Unity

Published March 28, 2014 by Lior Tal, posted by Liort
Do you see issues with this article? Let us know.
Advertisement
This post was originally shared on my blog at: http://www.tallior.com/code-generation-fun-with-unity/ Many Unity APIs use a string identifier, such as the game object's tag for various things (e.g: checking for a collision with "player"). In this article I explore a different, safer and automated technique that achieves the same result, without using strings.

The String Problem

Consider the following code: var player = GameObject.FindWithTag("Player"); The code is not type safe: it relies on a string identifier to perform an object lookup. This identifier may change, making this code "out of sync" with the project, or be misspelled, making the code fail. In addition, this string might be used in many different locations of the code, increasing the risk of previous mentioned concerns.

A Solution "Sketch"

A possible solution to this issue is to create a static helper class that will expose all tags as public (static) fields. When needed, instead of using a string, we'd use the class's static fields: public static class Tags { public static readonly string Player = "Player"; } Accessing this tag is safer now, since we're not (directly) relying on the string representation of the tag: var player = GameObject.FindWithTag(Tags.Player); Effectively, the code will operate the same as before, but now we have a single location where the tag is declared. There are 2 main issues with this approach:
  1. In case there are many tags defined in the project, creating the helper class can be a somewhat tedious task (creating a field per tag).
  2. In case a tag's name changes in the Unity editor, you have to also remember to replace its corresponding value in the helper class.
It seems that this solution is a step in the right direction, but we need some extra "magic" to make it perfect.

Code Generation To The Rescue

Code generation is an (oftentimes) overlooked practice, where code is being automatically generated by some other code, a template or some tool. In particular, code generation really shines in cases where we want to generate long, repetitive code from an underlying data source. Translating this to the problem described above, we would like to generate a static helper class with many static fields from an underlying data source (a collection with all of the project's tags). 46974122-300x271.jpg To achieve this, we'll use one particular implementation of a code generation engine called T4, which is a template engine that comes with Visual Studio (which also heavily relies on it for various tasks), and also comes out of the box with Mono (yes, the same one that is installed with Unity). A T4 template is a file (with a .tt extension) that mixes a body of text with special directives. The template generates a single output file (usually, a code file, although it can generate any other file format).

T4 Templates

In order to add a T4 template to your project, right click on your code project in MonoDevelop, and select: Add->New File. T4 Templates can be found under Text Templating on the left: t41.png

T4 Template Types

There are 2 types of available templates (ignore Razor templates as they're irrelevant for this discussion):
  • T4 Template - a template file that gets transformed during compilation time into the output file. This type of template is used to generate code files that are needed at design time (e.g: think of Microsoft's Entity Framework, where a set of classes can be generated at design time from a database, instead of being created manually by the developer).
  • Preprocessed T4 Template - a template file that creates an "intermediate" class that can later be used to generate the output code file.

Unity Bug

Unity currently does not support adding T4 templates (.tt files) to scripting code - after compilation, all .tt files will be dropped from the code project (I reported this bug here. This forces us to use option #2 - creating a one-time "intermediate" class. This class will be used by a Unity edior extension, from which we can generate the class we want and add it to the project.

Show Me the Code!

Here is the preprocessed T4 template that will generate the Tags class for us (although the provided sample uses the same template to generate a Layers class in exactly the same manner): t4_example.png A few things that should be noted:
  1. Any text not contained within <# #> tags is being output as is.
  2. The template is a preprocessed template. This means it does not generate an output code file directly. Instead, it generates an intermediate (partial) class with a TransformText() method that returns the template final output (the text with the generated class).
  3. The code prints out a header (the class declaration with some comments), then it iterates all elements in source and outputs a public static read-only field for each item (does a small manipulation to make sure the field name does not have spaces in it).
  4. The variables classname, item and source are actually implemented in a code file (a partial class with the same name as the template class. Remember I said the template generates a partial class? this allows mixing the template with some custom code. For more clarity, see the full code in the link below).

In Conclusion

This article aimed to open a hatch to the wonderful world of code generation (and T4 in specific), while showing how it can solve real world problems in a short and simple way. I did not dive into T4 syntax or more advanced topics (leaving it for you to explore, or as a subject for future articles). For more information regarding T4 - see the links below.

Links

  • Unity package that demonstrates the solution from this post - http://goo.gl/ijXeD0
  • Full code demonstrated in this post - https://bitbucket.org/liortal/unity-t4
  • Runtime text generation with T4 - http://msdn.microsoft.com/en-us/library/ee844259.aspx
  • Preprocessed T4 templates - http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/
Cancel Save
0 Likes 7 Comments

Comments

wicked357

Awesome article. I just started using Unity and I think this will help me down the road.

March 27, 2014 07:49 PM
Irlan Robson

"What if I told you."

March 27, 2014 08:35 PM
SolDirix

Cool :3 This is very helpful.

March 29, 2014 03:05 AM
stevenmarky

This is interesting, I actually wrote an editor script a while ago which generates a C# file containing an enum list from a certain type of file resource, so if those resource files are ever removed we'd get compilation errors instead of waiting for a runtime error. What advantage would using T4 code generation have over manually writing a C# file?

April 03, 2014 03:18 PM
Liort

@stevenmarky How did you generate the C# file? did you manually output strings with data that formed C# code?

T4 has a nice "what you see is what you get" structure to it -- you can create your class with some markups that will be replaced by some functions or code, and you get to keep all the layout and indentation (see the screenshot i put of the T4 code and how it looks almost like a normal class).

April 03, 2014 05:19 PM
andrew47

I wrote an Editor util that essentially does this, without the use of T4.

One of the issues you'll run into using T4 for the Tags is if the Tag name is not a validate identifier in C#. You'll end up with a broken class. This util ensures that can't happen, whilst also remaining entirely configuration from the Project Settings.

https://github.com/AlkimeeGames/TagLayerTypeGenerator

April 12, 2021 11:32 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement