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. Dismiss Notice

Question Check when dynamic script files are added to assembly

Discussion in 'Scripting' started by charles_xl, May 15, 2021.

  1. charles_xl

    charles_xl

    Joined:
    Oct 4, 2016
    Posts:
    87
    Hey all,

    I'm running into an issue and have been banging my head against the wall on this one for a while. I have custom script (and asmdef) templates which are just text files that I can inject certain variables in and then convert to a cs/asmdef file. These files are created with:
    Code (CSharp):
    1. ProjectWindowUtil.CreateAssetWithContent
    Once all files get generated, I'm trying to catch when these new files will be added to the assembly, but the first compilation after these are created, the assemblies are missing these new files. I am forcing compilation with:
    Code (CSharp):
    1. CompilationPipeline.RequestScriptCompilation()
    Only after further compilation are they caught so I'm wondering is there a better way to create dynamic cs/asmdef files like this and be able to catch when they're added to the assembly?

    I'm running an editor tool currently that can't proceed properly until those scripts get added but if it's not caught after the first compilation the tool just fails.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  3. charles_xl

    charles_xl

    Joined:
    Oct 4, 2016
    Posts:
    87
    Yes, so after I create all the files, I am calling both
    AssetDatabase.Refresh
    and
    AssetDatabase.SaveAssets
    . I'm wondering if it has something to do with not creating the script via
    AssetDatabase
    , however I don't think scripts can be created that way.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Shouldn't matter. I use this for all my codegen stuff and it works flawlessly. Now granted I haven't tried it with asmdefs but what I do all day long is a blurb of C# code writes a fresh .cs file out, then hits AssetDatabase.Refresh() and that compiles all the changes in that file. Since that triggers the compile, I imagine the asmdefs would be respected afresh.
     
  5. charles_xl

    charles_xl

    Joined:
    Oct 4, 2016
    Posts:
    87
    I'm wondering if there's a different way to force the assemblies to reload, I'm just listening for
    CompilationPipeline.compiltationFinished
     
  6. charles_xl

    charles_xl

    Joined:
    Oct 4, 2016
    Posts:
    87
    Is there a better API to use than just
    AppDomain.CurrentDomain.GetAssemblies()
    ?