Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Question Trying to get example SourceGenerator to work in 2020.3.30

Discussion in 'Experimental Scripting Previews' started by PhilSA, Apr 5, 2022.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    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
     
    Last edited: Apr 5, 2022
  2. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    This version does not have support of source generators. 2021.2 is the first version with source generators support
     
  3. Anthiese

    Anthiese

    Joined:
    Oct 13, 2013
    Posts:
    73
    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.
    Code (csharp):
    1. labels:
    2. - RunOnlyOnAssembliesWithReference
    3. - RoslynAnalyzer
    4. - 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.
     
    PhilSA likes this.
  4. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    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!
     
    Anthiese likes this.
  5. Xaon

    Xaon

    Joined:
    Feb 28, 2013
    Posts:
    62
    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.
     
  6. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I've written clearer instructions here
    https://forum.unity.com/threads/sou...-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
     
    Last edited: Apr 14, 2022
    Xaon likes this.
  7. DrViJ

    DrViJ

    Joined:
    Feb 9, 2013
    Posts:
    163
    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

    Code (CSharp):
    1. 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
     
    Last edited: Jul 11, 2022
  8. wewear

    wewear

    Joined:
    Oct 18, 2021
    Posts:
    3
    I also tried the instructions with 2022.1.12 netstandard 2.1 and it's not working... Does somebody have some tips? :\
     
  9. the_9an

    the_9an

    Joined:
    Aug 31, 2020
    Posts:
    1
    TheNullReference and Xaon like this.
  10. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    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.
     
    TheNullReference and JesOb like this.
  11. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    268
    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!
     
  12. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    362
    On Unity 2022.3, I'm using Microsoft.CodeAnalysis.CSharp 4.1 without issue.
     
  13. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    268
    Is that the latest version? Mine worked going back to 3.8 but may well work with later versions as well.
     
  14. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    362
    Not the latest version of Microsoft.CodeAnalysis.CSharp. But 4.1 is the highest version that 2022.3 supports.
     
    TheNullReference likes this.
  15. apilola

    apilola

    Joined:
    Jul 27, 2018
    Posts:
    17
    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...

    upload_2023-8-6_13-11-13.png
     
  16. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    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.
     
    Last edited: Jan 1, 2024
  17. 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)
     
  18. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    362
    This is my setup. It works perfectly for many packages.
    upload_2024-1-2_9-23-59.png
     
  19. 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:
    Code (CSharp):
    1. public static readonly DiagnosticDescriptor FailedToParseMessage = new DiagnosticDescriptor(
    2.             "something error ID like SAMPLE001",
    3.             "Message title",
    4.             "Failed to parse message type '{0}'",
    5.             "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
    .
    Code (CSharp):
    1. ctx.ReportDiagnostic(Diagnostic.Create(
    2.                 FailedToParseMessage,
    3.                 whateverNode.GetLocation(),
    4.                 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):
    Capture.PNG
     
    DrViJ and CodeSmile like this.
  20. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Thanks, that's great info, I gotta try this out! :)