Unity3D-CodeGenerator icon indicating copy to clipboard operation
Unity3D-CodeGenerator copied to clipboard

Code Generator

GitHub package.json version GitHub issues GitHub pull requests GitHub license GitHub last commit

Provides a collection of classes that will aid you with generating code.

Installation

  1. The package is available on the openupm registry. You can install it via openupm-cli.
openupm add net.tnrd.codegenerator
  1. Installing through a Unity Package created by the Package Installer Creator from Needle

Usage

The following example is based on the package Layers & Tags Generator

public void Generate()
{
    var tags = InternalEditorUtility.tags;

    var generator = new Generator();
    var tagsClass = new Class("Tags");

    foreach (var tag in tags)
    {
        tagsClass.AddField(
            new Field(Utilities.GetScreamName(tag), tag, typeof(string))
        {
            IsConst = true,
        });
    }

    generator.AddClass(tagsClass);
    generator.SaveToFile(Application.dataPath + "/Generated/Tags.cs");

    AssetDatabase.Refresh();
}

In the example above you can see the generator being used.

It starts with a Generator, which holds all of the items that will be generated. In this example a class named Tags will be added.

The Tags class contains some simple fields that have the name of the tag in SCREAM_CASE and are assigned the value. The type of the fields are of string and they are marked as constant for easy access.

Support

Code Generator is an open-source project that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.

ko-fi

Contributions

Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.