Search Unity

Question Incorporate Unity with Source Generator - Unity was not able to compile with generated code

Discussion in 'Scripting' started by noobnewbier, Mar 21, 2021.

  1. noobnewbier

    noobnewbier

    Joined:
    May 24, 2017
    Posts:
    6
    Unity version : 2020.2.7f1
    IDE : Rider 2020.3.2

    I am having problems when I am trying to incorporate Unity with Source Generator.

    I am able to build a source generator and put the resulting analyzer in Unity, my IDE is able to pick up the generated code(syntax highlighting is working), however when unity compiles it it was not able to find the related code and hence give an compile error(screenshot is what I can see from myside).

    It seems people are having similar problems but was not able to find a solution. I tried to force Unity to use a newer version of Compiler(3.10) using third party library but that doesn't seems to fix the issue either.

    To the best of my understanding the issue seems to be the compiler Unity is using is outdated, but I would assume my hack of forcing it to use a newer version of compiler should have solved the issue if that's the case.

    What is the reason that it the source generator is not working, and how could I get around this? I don't mind using hacks as the project is not commercial.
     

    Attached Files:

    Last edited: Mar 21, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Generated code will work, but after writing the generated file, make sure you call:

    Code (csharp):
    1. AssetDatabase.SaveAssets();
    2. AssetDatabase.Refresh();
    But obviously that is also assuming your code generator is 100% perfect and producing functional code (and here's the really important part) that Unity can compile given the .NET and compiler settings. If it is newfangled whacky latest bleeding edge C# code syntax, it might not work under Unity.
     
  3. noobnewbier

    noobnewbier

    Joined:
    May 24, 2017
    Posts:
    6

    Hi, thanks for your reply.

    I am not sure I understand what you are referring to - at the moment I am pretty sure Source Generator's generated source code sits within my memory, quote

    File output is not required for the correct function of either command line or IDE based generation, and can be completely disabled, if required. The IDE will work on in-memory copies of the generated source texts (for 'Find all references', breakpoints etc.) and periodically flush any changes to disk.

    Do you mean that I should try to find a way to make sure the generated code is written to the hard disk? I am kind of trying to avoid it as it seems to me it requires me to use MSBuild, which I don't think Unity is using.(having said that I also have no idea how this should work, and is willing to give anything a try)

    If you have experience with this, do you mind sharing in details?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I have no idea about any of that source generation stuff. Unity is simple and wants you to put code where it can compile it, which is one of the most-awesome things about it. No Microsoft weird wiggy C# assembly crud to worry about, unless you like that sort of misery.

    Remember, absolutely NOTHING that goes on in Visual Studio makes a hill of beans to Unity. You may press "BUILD" in VS and get a good build, but Unity does not care.

    Unity does its own compilation, so you technically don't even need any IDE. You can edit your code in Notepad or vi and Unity is happy to take care of everything for you.