Version: Unity 6 Preview (6000.0)
Language : English
Roslyn analyzers and source generators
Install and use an existing analyzer or source generator

Create and use a source generator

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.

Note: Unity only supports version 6.0.0-preview of the System.Text.Json namespace. If you want to use this namespace in your application, ensure you use version 6.0.0-preview. For more information about System.Text.Json, refer to Microsoft’s System.Text.Json Namespace documentation.

To create a source generator in Visual Studio and then apply it for use in your Unity project:

  1. In Visual Studio, create a C# class library project that targets .NET Standard 2.0 and name the project ExampleSourceGenerator.

  2. Install the Microsoft.CodeAnalysis.Csharp NuGet package for the project. Your source generator must use Microsoft.CodeAnalysis.Csharp 4.3 to work with Unity.

  3. In your Visual Studio 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) { }
        }
    }
    
  4. Build your source generator for release. To do this, go to Build and select the Batch Build option, then select the Release option and Build.

  5. In your source generator’s project folder, find the bin/Release/netstandard2.0/ExampleSourceGenerator.dll file.

  6. Copy this file into your Unity project, inside the Assets folder.

  7. Inside the Asset Browser, click on the .dll file to open the Plugin Inspector window.

  8. Under Select platforms for plugin, disable Any Platform.

  9. Under Include Platforms, disable Editor and Standalone.

  10. Under Asset Labels, click on the label icon to open the Asset labels sub-menu.

  11. Create and assign a new label called RoslynAnalyzer. To do this, type RoslynAnalyzer in the text input fieldA field that allows the user to input a Text string More info
    See in Glossary
    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.

  12. A warning will be printed in the console because this source generator will get injected into more than one assembly. The solution is to make ExampleSourceGenerated in the above example internal or the name itself should be generated.

  13. 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);
        }
    }
    
  14. Add this script to a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    in the sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
    See in Glossary
    and enter Play mode. A message from the source generator will appear in the Console windowA Unity Editor window that shows errors, warnings and other messages generated by Unity, or your own scripts. More info
    See in Glossary
    , including the time stamp. A warning will also appear in the console because this source generator will get injected into more than one assembly. The solution is to make ExampleSourceGenerated in the above example internal or the name itself should be generated.

For more information about source generators, refer to Microsoft’s Source Generators documentation.

Additional resources

Roslyn analyzers and source generators
Install and use an existing analyzer or source generator