Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Source Generator - access AssetDatabase in ISourceGenerator

Discussion in 'Asset Database' started by paparape, May 11, 2022.

  1. paparape

    paparape

    Joined:
    Apr 5, 2022
    Posts:
    4
    Hello. Title is self-explanatory. Is there a way to access AssetDatabase and other UnityEditor apis in Roslyn source generator to traverse entities and generate code based on results?

    Particular usecases I want to achieve:
    • generate convinient monobehaviour wrappers for gameobjects automatically based on hierarchy
    • analyze if MySuperPuperCreateEntity(string prefabPath) function accepts really existing prefab
     
    Last edited: May 11, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    I do this in my Datasacks package, iterating assets (in this case ScriptableObjects called Datasacks) and producing static classes according to what it finds, and then generating a live index of them all.

    It's in this file:

    https://github.com/kurtdekker/datas...cks/Assets/Datasack/Core/DatasackInspector.cs

    Look for the
    GenerateCode()
    method.
     
    Bunny83 likes this.
  3. paparape

    paparape

    Joined:
    Apr 5, 2022
    Posts:
    4
    The question was about c# source code generators - https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Correction. Your question was actually:

    I answered above with an example that uses both the AssetDatabase and UnityEditor.

    If you have questions about a Microsoft/Roslyn product, you may wish to visit a board that supports that product.
     
  5. paparape

    paparape

    Joined:
    Apr 5, 2022
    Posts:
    4
    But title contains ISourceGenerator. Anyway, edited original message to indicate that I am interested in Roslyn source generators, which are part of 2021 lts release.
     
  6. biziclop42

    biziclop42

    Unity Technologies

    Joined:
    Oct 25, 2016
    Posts:
    2
    Hi, I don't have and answer for you, but I think the folks over scripting are more likely to be able to help you.
     
  7. TommyTheITGuy

    TommyTheITGuy

    Joined:
    Jun 11, 2015
    Posts:
    49
    You can't. The generator runs in a separate process from Unity during compilation. Your best bet would be to hook up to CompilationPipeline.compilationStarted event, gather the data needed by the generator, save it in a file and read from that file while the generator is working.