[Tutorial]Code Templates for all languages: CSharp, JavaScript, Boo, Shader and ComputeShader

CREATE CODE TEMPLATES

VIDEO TUTORIAL(spanish text).

Project download link.

Translation:

Used to modify the original code inserted by Unity. We may include libraries, and other methods or references.

For example, we have a base class with abstract and virtual methods. We can prepare a code template with the methods that we can or should override.

Process:

  1. To create the template, Create a script as we would normally, named ‘template’ for example.

Compile(Save) it, to check some error, and finally substitute the name of the class to #CLASSNAME# label and save it as .txt

#CLASSNAME# is the name of the label that I have chosen. You can modify in ApplyTemplate class (in CodeTemplate namespace).

  1. Delete ‘template’ script. Only we need .txt file.

  2. The following will create access menu. To create shortcuts to different languages of code that can be created in Unity, I created classes for each type of language(BooTemplates, CSharpTemplates, etc…)

We create shorcuts menu in Create–>Templates–>Language(CSharp, Boo, …) using MenuItem before public static method in corresponding class(csharp template in CSharpTemplates, javascript template in JavaScriptTemplates, etc…).

Use:

  1. Create a script as we would normally. Then, with the new script selected in Project window, We apply the template found in the menu Create–> Templates, we created earlier.

This sounds like a lot more work than copying and pasting. Seems like one of those workflow optimizations that actually takes more time than it saves.

I find it useful, especially when sharing code.

One way to store non-compiled code (.txt) that can be used(or not) repeatedly in the same project, and other projects.

For example, control templates for Rigidbody or CharacterController component using the component Animator or Animation, GUI templates, templates for triggers, etc …