Create and use a source generator
Create a simple source generator and configure the Unity Editor to use it for your Unity project code.
Read time 3 minutesLast updated 12 days ago
You can use source generators as an additional step in your script compilation process, to add new code while you compile your existing code. As with code analyzers, you can use existing source generators or create your own.
To create a source generator in your IDE and then apply it for use in your Unity project:
-
In your IDE, create a C# class library project that targets .NET Standard 2.0 and name the project.
ExampleSourceGenerator -
Install theNuGet package for the project. Your source generator must use Microsoft.CodeAnalysis.Csharp 4.3 to work with Unity.
Microsoft.CodeAnalysis.Csharp -
In your IDE project, create a new C# file and add the following code:using Microsoft.CodeAnalysis;using Microsoft.CodeAnalysis.Text;using System.Text;namespace ExampleSourceGenerator{ [Generator] public class ExampleSourceGenerator : ISourceGenerator { public void Execute(GeneratorExecutionContext context) { System.Console.WriteLine(System.DateTime.Now.ToString()); var sourceBuilder = new StringBuilder( @" using System; namespace ExampleSourceGenerated { public static class ExampleSourceGenerated { public static string GetTestText() { return ""This is from source generator "); sourceBuilder.Append(System.DateTime.Now.ToString()); sourceBuilder.Append( @"""; } }}"); context.AddSource("exampleSourceGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); } public void Initialize(GeneratorInitializationContext context) { } }}
-
Build your source generator with a release build configuration.
-
In your source generator's project folder, find thefile.
bin/Release/netstandard2.0/ExampleSourceGenerator.dll -
Copy this file into your Unity project, inside thefolder.
Assets -
Inside the Asset Browser, click on the .dll file to open the Plugin Inspector window.
-
Under Select platforms for plugin, uncheck Any Platform.
-
Under Include Platforms, uncheck Editor and Standalone and any other checked platforms.
-
Under Asset Labels, click on the label icon to open the Asset labels sub-menu.
-
Create and assign a new label called RoslynAnalyzer. To do this, typein the text input field of the Asset labels sub-menu and press enter. This label must match exactly and is case sensitive. Once created, the label appears in the Asset labels sub-menu from then on. You can click on the name of the label in the menu to assign it to other analyzers.
RoslynAnalyzer -
To test the source generator is working, create a new MonoBehaviour script in the Editor with the following code:using UnityEngine;public class HelloFromSourceGenerator : MonoBehaviour{ static string GetStringFromSourceGenerator() { return ExampleSourceGenerated.ExampleSourceGenerated.GetTestText(); } // Start is called before the first frame update void Start() { var output = "Test"; output = GetStringFromSourceGenerator(); Debug.Log(output); }}
-
If the source generator is injected into more than one assembly, your IDE displays a compiler warning about conflicts (CS0436). To remove this warning, you can do one of the following:
- Make the class in the
ExampleSourceGeneratedexampleExampleSourceGenerator. Important: in this case you can only access the generated class from within the same assembly.internal - Change the name of the class so that it's unique to a given assembly, for example
ExampleSourceGenerated.ExampleSourceGenerated_MyAssembly
- Make the
-
Add this script to a GameObject in the scene and enter Play mode. A message from the source generator appears in the Console window, including the time stamp.
For more information about source generators, refer to Microsoft's Source Generators documentation.