Trying to get example SourceGenerator to work in 2020.3.30

I’m following the manual page on SourceGenerators and trying to get this example working, but I’m in Unity 2020.3.30 (I need this for DOTS). After following the manual page’s example, I get this compilation error:

“ExampleSourceGenerated” is the class that’s supposed to be generated in this example. In VisualStudio, I can definitely navigate to the generated “ExampleSourceGenerated” file and see that everything is properly generated. When I try to use the generated class in my code, autocomplete is working and I get no red squiggle lines.

However, I’ve tested with the same files in 2021.2 and it works fine. So it seems the real problem is 2020.3.30. Is there any workaround to get SourceGenerators working in 2020.3.30? The source generators in the Entities 0.50 package are working well in this version, so I’m hoping there might be something we can do about this

This version does not have support of source generators. 2021.2 is the first version with source generators support

As far as I can tell (very minimally tested my lua glue generator) it works as long as you also add a SourceGenerator label as well in the .meta. I don’t see any files from it being generated under Temp/GeneratedCode, but the code compiles and the stuff works in Play Mode.

labels:
- RunOnlyOnAssembliesWithReference
- RoslynAnalyzer
- SourceGenerator

Of course this is definitely not a supported scenario, really just a holding action until 0.51+2021.2.

Just so you’re aware, the functionality of this actually comes from com.unity.roslyn - it’s not to do with the core Editor.

Awesome, it works! I also had to make sure the sourceGenerator .DLL and the code that uses it are in an .asmdef

The only issue I see is that in Visual Studio, I get red squiggels under generated stuff, but no compile errors

thanks!

Any follow ups on that? I’m at the same point. Source generator works when .dll and game code is under the same asmdef, but neither VS not Rider recognises the generated code. Makes hard to work on the project.

When I don’t use asmdef IDE sees stuff but the code is generated for each assembly so nothing works due to duplicated definitions.

I’ve written clearer instructions here
https://forum.unity.com/threads/sources-included-polymorphism-in-dots-now-with-source-generators.1264616/#post-8031299

At the top of that thread I linked to, there’s a sample project where I have a source generator generating code from code in another .asmdef referencing it, if you want to try to use that project as reference. The “PolymorphicStructsSourceGenerator”, which is part of “PolymorphicStructs.asmdef”, generates code based on structs in “MyState”, which is part of “TestAssembly.asmdef”

And here’s what PolymorphicStructs.asmdef and TestAssembly.asmdef look like:

Yesterday I started a new project where I needed source generators, and I re-followed those instructions I wrote in the linked post above. Everything worked from the start, so I think if you make sure to do exactly that, it might work for you as well.

If VS can’t find the generated code, just try closing & reopening VS, and wait for the little “Background Tasks” icon in the bottom left to say it’s ready before you search

Hi! I am sorry for bumping this thread, but how to make it work in Unity? I am trying Unity 2022.1.8f1, Roslyn Analyzers are enabled. But it looks like Generator does not work. I Imported Microsoft.CodeAnalysis, Microsoft.CodeAnalysis.CSharp and added an assembly. Then I created script with code from example (ExampleSourceGenerator from documentation) and created another assembly with code from HelloFromSourceGenerator, but I keep getting

error CS0103: The name 'ExampleSourceGenerated' does not exist in the current context

Maybe the Generator code should be triggered somehow? I added labels to all dlls (RoslynAnalyzer and etc.), I tried to place generator and its usage in the same and in different assemblies, but that did not work. Can the version of Microsoft.CodeAnalysis be the problem? I’m stuck at the very beginning with a simple sourcegenerator and it’s not clear how to move on.

The only difference from my way to use it is that I want to make Generator in my script, not in precompiled dll as example shows

p.s. It looks like one of reasons is netstandard 2.1 in Unity 2022, the official documentation says the only supported version to work with SourceGenerators is 2.0

I also tried the instructions with 2022.1.12 netstandard 2.1 and it’s not working… Does somebody have some tips? :\

@wewear

I also tried the instructions with 2022.1.12 netstandard 2.1 and it’s not working… Does somebody have some tips? :\

Make sure that you used right Microsoft.CodeAnalysis version (3.8)

I had luck with source generators targeting Roslyn 3.11 on older Unity versions.
For anything targeting 4.X and up only Unity 2022.2 supports it.

Just wanted to say I was having a lot of trouble getting the example source generator to work in version 2022.3.4f1 and can confirm the issue was not using the exact 3.8 version of the Microsoft Code Analysis. Thank you!

On Unity 2022.3, I’m using Microsoft.CodeAnalysis.CSharp 4.1 without issue.

Is that the latest version? Mine worked going back to 3.8 but may well work with later versions as well.

Not the latest version of Microsoft.CodeAnalysis.CSharp. But 4.1 is the highest version that 2022.3 supports.

I can’t get unity to recognize any code generators.

I’ve tried building it with both Microsoft.CodeAnalysis 4.1 and 3.8.

I’m using Visual Studio 2022

Nothing seems to work…

To add to this, facing the same “does not exist in current context” issue for Unity 2022.3, I was also able to resolve this by downgrading to CodeAnalyzer 4.1 and 3.8 also works for me.

.netstandard 2.1 throws an error, you have to use .netstandard 2.0

Do NOT install the com.unity.roslyn package - this is not needed, it’s probably built in now.

Add the “RoslynAnalyzer” label to the DLL file. The “SourceGenerator” label is NOT needed.

Place the files as follows and it should work:
Assets/Plugins/SourceGenerator.dll
Assets/Scripts/HelloFromSourceGenerator.cs
Assets/Scripts/AnyAssembly.asmdef

Do NOT add an AsmDef in the DLL folder unless you also add at least one script to that AsmDef too!

The generator DID NOT WORK for me when I had only an AsmDef and DLL in the Plugins folder, not even when I referenced this AsmDef. It started to work again only after adding a script to the Plugins folder (with the reference to the Plugins AsmDef in the Scripts AsmDef).

Noteworthy:
I created the project following the manual docs. I specifically used MS Visual Studio 2022 to do that because I just did not want to bother finding the right steps in Rider. I explicitly installed the 3.8 version of the CodeAnalyzer. At least so I thought … after checking again: no, it used 4.8! So double-check that CodeAnalyzer version.

FYI: there aren’t any. Create the project with a setup you like, make sure it has the .netstandard2.0 setting, install the appropriate nuget packages (Microsoft.CodeAnalysis.Common and the Microsoft.CodeAnalysis.CSharp), create your generator, build, copy, Unity setup (label and whatnot) and you’re done.

I have this example, it is tried and working.
https://github.com/LurkingNinja/DomainReloadSG (currently w/ CodeAnalysis packages 4.1)

This is my setup. It works perfectly for many packages.
9556960--1351069--upload_2024-1-2_9-23-59.png

BTW.

IIncrementalGenerator flavor.

Unity: 2022.3.16f1
Microsoft.CodeAnalysis.Common and Microsoft.CodeAnalysis.CSharp: 4.3.1
Everything is working.
Unity setup is removed all platform check marks and added the RoslynAnalyzer label only.

You can also send message into Unity’s Console in order to debug doing the following:
Declare all messages like this one:

public static readonly DiagnosticDescriptor FailedToParseMessage = new DiagnosticDescriptor(
            "something error ID like SAMPLE001",
            "Message title",
            "Failed to parse message type '{0}'",
            "Parser", DiagnosticSeverity.Error, true);

Obviously configure them as you wish.

Then in the Execute (or similarly named), where you have SourceProductionContext.
You can just call ReportDiagnostic.

ctx.ReportDiagnostic(Diagnostic.Create(
                FailedToParseMessage,
                whateverNode.GetLocation(),
                whateverNode.ToString()));

Obviously you can configure the diagnostics many other way depending on the message you want to send.

The result is a standard error message (in this case):

Thanks, that’s great info, I gotta try this out! :slight_smile: