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. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Incremental Source Generator csproj Template (2022.2.b13)

Discussion in '2022.2 Beta' started by unity_IpxdANggCs1roQ, Nov 8, 2022.

  1. unity_IpxdANggCs1roQ

    unity_IpxdANggCs1roQ

    Joined:
    Feb 19, 2018
    Posts:
    44
    I share a csproj configuration that runs Incremental Source Generator in Unity version 2022.

    Unity2022.2.b13 uses version 4.1.0 of Microsoft.CodeAnalysis.CSharp.

    Below are the the csproj that I have confirmed to work (Unity2022.2.b13).
    Label the imported dll as RoslynAnalyzer and uncheck, auto reference, all platforms . No other special configuration is required. It works by default.
    Code (CSharp):
    1. <Project Sdk="Microsoft.NET.Sdk">
    2.  
    3. <PropertyGroup>
    4.   <TargetFramework>netstandard2.1</TargetFramework>
    5.   <LangVersion>latest</LangVersion>
    6.   <IncludeBuildOutput>false</IncludeBuildOutput>
    7.   <DevelopmentDependency>true</DevelopmentDependency>
    8.   <IsRoslynComponent>true</IsRoslynComponent>
    9.   <AssemblyName>YourGeneratorName</AssemblyName>
    10. </PropertyGroup>
    11.  
    12. <ItemGroup>
    13.   <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" PrivateAssets="all"/>
    14. </ItemGroup>
    15.  
    16. <ItemGroup>
    17.   <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
    18. </ItemGroup>
    19.  
    20. </Project>
    21.  
    Note : As far as I tested, Microsoft.CodeAnalysis.CSharp version 4.3.1 did not work (2022.2.x) , and 4.x.x does not work with Unity2021, so you will have to create dlls separately for Unity2021 and Unity2022.
     
    Last edited: Nov 8, 2022
    oscarAbraham and Luxxuor like this.
  2. Kinggrass

    Kinggrass

    Joined:
    Jun 7, 2016
    Posts:
    72
    @unity_IpxdANggCs1roQ
    Do you know if it is possible to make it part of a package?
    Unfortunatly Unity does not feature to label assets in packages or that they can be packed with labels.
     
  3. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    Yes they can be part of a package, Unity uses SG themselves in some of their packages (mainly Entities). When I add a SG to my internal package then I first import it into an Assets folder and then make sure I copy it together with its meta file to the package location; you can't edit the labels in the inspector but you can modify the meta file just fine.
     
    Kinggrass likes this.
  4. Kinggrass

    Kinggrass

    Joined:
    Jun 7, 2016
    Posts:
    72
    @Luxxuor

    upload_2022-11-23_11-34-8.png

    Thanks this is helping :)
    Do you also know if it is possible to generate MonoBehaviours with it or also editor extensions?
    The MonoBehaviour is shown it the menu but if I want to add it, it tells that the file can not be found?

    Should I take another way for that approach?
    Can I somehow check for changes in files and generate/override c# files like MonoBehaviours?
     
    Last edited: Nov 23, 2022
  5. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    Technically you can generate any code you want, for MonoBehaviours though you have to make sure the generated file name is exactly like the MB name (same restriction goes for non-generated code).

    Personally I would rather require an user created MB that is partial and then let the SG generate any code that you want. Its easier for the user to understand and less black-boxy, you will also have much less problems with losing serialized data on GOs with these MBs when the name gets changed etc.
    And yes overriding is usually done through making things partial.
     
    Kinggrass likes this.
  6. Kinggrass

    Kinggrass

    Joined:
    Jun 7, 2016
    Posts:
    72
    @Luxxuor
    Thanks for your response.
    I think I will drop the Source Generator at first until it is supported by Unity that it can generate files inside the project and not only for the compilator.
    Source Generators in a native C# Project can be adjustet by a setting in the solution file that it produces files in a given path and that is what I need in Unity but thats not working there.

    I looked at the Github Repository of the InputManager they are also doing Code Generation way before the C# SG and I think I will go the same approach.

    Tank you again for your responses here and the discussion :).
     
  7. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    That will never happen as SG are a C# feature, they are also not meant to create actual tangible files, as their outputs could - at any time - change their filename or vanish.
    You can change the filename that a SG generates by changing the source code of the SG itself, there is no setting in a solution file for it.
     
  8. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    164

    You are a hero! This worked for me after messing around for like 2 hours with the unity docs. This is the correct way to do it.