|  | 
| 2 | 2 | 
 | 
| 3 | 3 | [](https://github.com/sandrofigo/Simple-Code-Generator-Unity3D/actions/workflows/tests.yml) | 
| 4 | 4 | 
 | 
| 5 |  | -A plugin for generating source code from templates in Unity | 
|  | 5 | +A library for generating source code from templates in Unity | 
| 6 | 6 | 
 | 
| 7 |  | -> [!WARNING] | 
| 8 |  | -> This repository is still WIP! | 
|  | 7 | +--- | 
|  | 8 | + | 
|  | 9 | +## Installation | 
|  | 10 | + | 
|  | 11 | +- Install [OpenUPM CLI](https://github.com/openupm/openupm-cli#installation) | 
|  | 12 | +- Run the following command in your Unity project folder | 
|  | 13 | + | 
|  | 14 | +  ``` | 
|  | 15 | +  openupm add com.sandrofigo.simplecodegenerator | 
|  | 16 | +  ``` | 
|  | 17 | + | 
|  | 18 | +## Usage | 
|  | 19 | + | 
|  | 20 | +Create a `public static void` method in a class anywhere in your Unity project and decorate it with the `[CodeGenerationMethod]` attribute. This will make the method visible to the code generator. If you want to expose the method to the "Code Generation" menu add a `[MenuItem(...)]` attribute. | 
|  | 21 | + | 
|  | 22 | +```csharp | 
|  | 23 | +public static class MyClass | 
|  | 24 | +{ | 
|  | 25 | +    [CodeGenerationMethod] | 
|  | 26 | +    [MenuItem("Code Generation/Generate My Code")] | 
|  | 27 | +    public static void GenerateMyCode() | 
|  | 28 | +    { | 
|  | 29 | +        var data = new | 
|  | 30 | +        { | 
|  | 31 | +            Text = "Hello World!", | 
|  | 32 | +            Colors = new[] { "Red", "Green", "Blue" }, | 
|  | 33 | +            Animal = new | 
|  | 34 | +            { | 
|  | 35 | +                Type = "fox", | 
|  | 36 | +                Color = "brown", | 
|  | 37 | +            } | 
|  | 38 | +        }; | 
|  | 39 | + | 
|  | 40 | +        CodeGenerator.GenerateFromTemplate("Scripts/Templates/MyTemplate.txt", "Scripts/Generated/MyTemplate.generated.cs", data); | 
|  | 41 | +    } | 
|  | 42 | +} | 
|  | 43 | +``` | 
|  | 44 | + | 
|  | 45 | +Additionally you need a template file in your project which the code generator can use. | 
|  | 46 | +Value names are case sensitive e.g. if you want to insert the value of the `data.Text` property in the data object you can do this by surrounding it with `{{  }}`. Accessing nested properties is the same as in C# e.g. `Animal.Color`. | 
|  | 47 | + | 
|  | 48 | +``` | 
|  | 49 | +// This is a sample template | 
|  | 50 | +
 | 
|  | 51 | +// The value of the property "Text" is: {{ Text }} | 
|  | 52 | +
 | 
|  | 53 | +// The quick {{ Animal.Color }} {{ Animal.Type }} jumps over the lazy dog | 
|  | 54 | +
 | 
|  | 55 | +{{ for color in Colors }} | 
|  | 56 | +
 | 
|  | 57 | +public class My{{ color }}Class | 
|  | 58 | +{ | 
|  | 59 | +    // Some implementation | 
|  | 60 | +} | 
|  | 61 | +
 | 
|  | 62 | +{{ end }} | 
|  | 63 | +``` | 
|  | 64 | + | 
|  | 65 | +The generated file `MyTemplate.generated.cs` will look like this: | 
|  | 66 | + | 
|  | 67 | +```csharp | 
|  | 68 | +// This is a sample template | 
|  | 69 | +
 | 
|  | 70 | +// The value of the property "Text" is: Hello World! | 
|  | 71 | +
 | 
|  | 72 | +// The quick brown fox jumps over the lazy dog | 
|  | 73 | +
 | 
|  | 74 | + | 
|  | 75 | +public class MyRedClass | 
|  | 76 | +{ | 
|  | 77 | +    // Some implementation | 
|  | 78 | +} | 
|  | 79 | + | 
|  | 80 | + | 
|  | 81 | +public class MyGreenClass | 
|  | 82 | +{ | 
|  | 83 | +    // Some implementation | 
|  | 84 | +} | 
|  | 85 | + | 
|  | 86 | + | 
|  | 87 | +public class MyBlueClass | 
|  | 88 | +{ | 
|  | 89 | +    // Some implementation | 
|  | 90 | +} | 
|  | 91 | + | 
|  | 92 | +``` | 
|  | 93 | + | 
|  | 94 | +Code generation is automatically triggered when a `*.cs`, `*.txt` or `*.json` file has changed in your project. Additionally you can trigger the code generation manually under the "Code Generation" menu item. | 
|  | 95 | + | 
|  | 96 | +## Community | 
|  | 97 | + | 
|  | 98 | +Support this project with a ⭐️, report an issue or if you feel adventurous and would like to extend the functionality open a pull request. | 
| 9 | 99 | 
 | 
0 commit comments