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
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Source Generators via Roslyn Analyzers in 2020.2

Discussion in '2020.2 Beta' started by massivebacon, Nov 11, 2020.

  1. massivebacon

    massivebacon

    Joined:
    Apr 24, 2014
    Posts:
    27
    I've been sort of hacking away at trying to get C# 9's source generator functionality working in Unity. I know that Unity doesn't support C#9, however source generators have been made back-compatible with older language versions and frameworks.

    So I've compiled a DLL containing a working source generator, imported the DLL and tagged as a RoslynAnalyzer, and do get proper intellisense in the code editor where it picks up the generated class. So the generator is sort of working (with no errors on DLL import or anything).

    However, though the compiler isn't finding any errors at edit time in the IDE, when I open unity I get "The name 'HelloWorldGenerated' does not exist in the current context".

    I'm a bit stumped as to what I should try next. I'm starting to think that something is going on with the way RoslynAnalyzers are tagged that the Generator is being put in some different compilation pipeline in Unity and the code it emits is being done after the normal process of script compilation.

    I've also found that Analyzers generally don't work unless there is a .asmdef file next to the scripts it affects, which I suspect is bumping up the script in the compilation order or something to be used before the DLLs are referenced? Not sure.

    I know this isn't a stated use-case for Roslyn analyzers that Unity supports, but I think getting this to work would be a huge boon for the development process globally. Would love to know what other people have tried and if anyone has any inklings about ways to get this working!
     
    Last edited: Nov 11, 2020
  2. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    291
    I am also interested in this. I wish we had a supported way to write/use/package source generators (especially since the DOTS team is supposedly experimenting with them for codegen already).
     
  3. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    wrenge likes this.
  4. massivebacon

    massivebacon

    Joined:
    Apr 24, 2014
    Posts:
    27
    This looks super interesting, I’ve never heard of/used this before. I’m worried about it producing a lot of project bloat though if used - do you have experience with it? And what are the chances Unity just bumps the Roslyn version for 2020.2?
     
  5. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    According to this comment most likely Unity 2020.2 will not get Roslyn 3.8.0
    https://forum.unity.com/threads/unity-c-8-support.663757/page-6#post-6344598
     
  6. Enderlook

    Enderlook

    Joined:
    Dec 4, 2018
    Posts:
    54
  7. EvansThomas

    EvansThomas

    Joined:
    Oct 11, 2019
    Posts:
    1
  8. massivebacon

    massivebacon

    Joined:
    Apr 24, 2014
    Posts:
    27
  9. swedishfisk

    swedishfisk

    Joined:
    Oct 14, 2016
    Posts:
    57
  10. dmo_unity347

    dmo_unity347

    Joined:
    Jun 1, 2021
    Posts:
    10
    Just want to throw it out there you actually can use source generators with Unity if you build your code as a managed dll. It is something I am doing currently and it has worked wonderfully.

    https://docs.unity3d.com/Manual/UsingDLL.html
     
    apkdev likes this.
  11. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    248
    How'd you get it to work?
    My Unity is not picking the generator up and I cannot access any generated content in scripts.
     
    DrViJ and guywald like this.
  12. HouinKyouma27

    HouinKyouma27

    Joined:
    Nov 13, 2017
    Posts:
    23
    Hi, I have the same problem, did you find a solution?
     
  13. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    248
    It really depends on the Unity version you are using. Unity 2020 iirc was kinda weird and inconsistent with SourceGenerators, but moving to later versions and following the https://docs.unity3d.com/Manual/roslyn-analyzers.html docs works perfectly.
     
    HouinKyouma27 likes this.