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
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.
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.
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
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!
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.
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.