Sytuacja kobiet w IT w 2024 roku
26.08.20203 min
Przemysław Błażyca
4Experience

Przemysław BłażycaUnity Developer4Experience

Unity script templates

Learn how to create useful and universal Unity script templates that increase your productivity.

Unity script templates

Almost every day in Unity we use script templates even if we don’t realize it. Every creation of new files like a script, shader, material uses a template to generate assets. Too often we generate a new script and start with deleting default code. So we should improve this process and we can do this with script templates. It is a small change, but it affects everyday work comfort.

Unity script templates are responsible for generating our assets with a predefined structure. In Unity, we usually start creating a new script by right-clicking and selecting Create/C# Script. The file is generated based on a pattern that looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class#SCRIPTNAME#: MonoBehaviour {
  // Start is called before the first frame update
  void Start() {#NOTRIM#
  }

  // Update is called once per frame
  void Update() {#NOTRIM#
  }
}


There are two keywords:

  • #SCRIPTNAME#– the file name you entered or the default one will appear here (for example: NewBehaviourScript).
  • #NOTRIM#– ensures that the whitespace is not deleted, so the script always contains a line between brackets.


It’s worth reviewing ready-made templates prepared by the Unity team. The template folder is part of the downloaded editor and is located in the directory:

…\Editor\Data\Resources\ScriptTemplates

Preparing your own templates to generate script files can facilitate everyday work in Unity. For a long time (checked on the Unity 2017 LTS version) it is possible to create your own patterns within your project. This allows you to use Unity script templates shared within one project using a version control system (e.g. Git).


Here’s how to do it

1. Create a folder ScriptTemplates under the main folder of Assets.

folder of assets


2. Create the file according to the pattern, preserving special characters:

PriorityNumberMenuPathDefaultName.FileExtension.txt

  • PriorityNumber – the lower the number, the higher template will appear in the context menu.
  • MenuPath – path in the context menu in the Create option – you can create categories by:


Custom Templates__Category__Script Template

  • DefaultName – the default name of the file.
  • FileExtension – the file extension with which it will be created.


Unity template examples:

81-C# Script-NewBehaviourScript.cs.txt

84-Shader__Unlit Shader-NewUnlitShader.shader.txt


3. Then restart the editor again. You can use the custom templates to create scripts.

custom templates

There is no need to copy all templates from the editor’s folder, nor do you need to create these files in the editor’s folder as in previous Unity versions.

Additional keywords:

  • #SCRIPTNAME_LOWER#– the first letter of the name is changed to a lower case.
  • #NAME#– It is no different from #SCRIPTNAME# – used in shader templates.


Expanding to own keywords

As part of expanding your own templates, you can create your own keywords. For this purpose, use the method called by Unity when creating a new script OnWillCreateAsset. Under this method, you can create your own rules on how to swap specific strings. In this way, you can, for example, create a namespace for classes, save the date of creation, add a script author, and expand the pattern mechanism as needed.

Examples of script templates

You can find some examples of useful and basic script templates below:

ScriptableObject class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "#SCRIPTNAME#", menuName = "#SCRIPTNAME#")]
public class #SCRIPTNAME# : ScriptableObject
{
    #NOTRIM#
}


Interface

public interface #SCRIPTNAME#

{
    #NOTRIM#
}


Serializable class

[System.Serializable]
public class #SCRIPTNAME#
{
    public #SCRIPTNAME# ()
    {

    }
}


JSON file

{
    "name": "#SCRIPTNAME#"
}


Empty MonoBehaviour class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour
{
    #NOTRIM#
}

Conclusion

Templates can be used to create simple scripts, for universal use. But you can also create a template for a specific project and share it with the development team. Dedicated templates can be created with basic logic, inheritance from classes in the project, etc.

Expanding them with your own keywords can create a useful tool for working with the Unity editor.



Publication from the website 4experience.co.

<p>Loading...</p>