Search Unity

Question Roslyn Source Generator Compatibility and import warnings

Discussion in 'Scripting' started by iamscottcab, Jun 5, 2023.

  1. iamscottcab

    iamscottcab

    Joined:
    Apr 11, 2017
    Posts:
    4
    This is a two part question.

    1. Is there a list of which version(s) of the Roslyn compiler are compatible with Unity? The documentation says you MUST use 3.8.0. But I also seem to have 3.9.0 working against Unity 2021.3.

    2. The reason I'd like to take advantage of 3.9.0 is to use the RegisterForPostInitialization hook to embed my attribute with my generator (as per this useful post). But I get warnings when I do that, specifically the following:

    Code (CSharp):
    1. warning CS0436: The type 'MyAttribute' in 'MyExample\MyExample.Generator\MyAttribute.g.cs' conflicts with the imported type 'MyAttribute' in 'PsdPlugin, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'MyExample\MyExample.Generator\MyAttribute.g.cs'.
    In my real project it says the conflict is within the VisualStudio.Unity package, so the package itself seems arbitrary. The example code that does this below.

    Code (CSharp):
    1. using Microsoft.CodeAnalysis;
    2.  
    3. namespace MyExample
    4. {
    5.     [Generator]
    6.     public class Generator : ISourceGenerator
    7.     {
    8.         private static readonly string _attribute = @"
    9. namespace A.B
    10. {
    11.    public class MyAttribute : System.Attribute { }
    12. }";
    13.  
    14.         public void Initialize(GeneratorInitializationContext context)
    15.         {
    16.             context.RegisterForPostInitialization(x => x.AddSource("MyAttribute.g.cs", _attribute));
    17.         }
    18.  
    19.         public void Execute(GeneratorExecutionContext context)
    20.         {
    21.             // Do stuff.
    22.         }
    23.     }
    24. }
    Is there something I can do with this code to prevent this warning, or is there an alternative way to embed it all in the one DLL to save me having to ship two (1 for the attribute and 1 for the generator)?