Search Unity

How can i Callback after the scripts compilation

Discussion in 'Editor & General Support' started by ExcellencyHong, Jan 31, 2020.

  1. ExcellencyHong

    ExcellencyHong

    Joined:
    Aug 9, 2016
    Posts:
    33
    using Unity 2019.3

    in editor, i'm trying to generate a cs file inheried the ScriptableObject and create a .asset file from it.

    i tried using the "UnityEditor.Compilation.CompilationPipeline.compilationFinished" and the "DidReloadScripts" attribute. but i cannot get a script as the MonoScript or cannot get the System.Type of original class in the MonoScript through MonoScript.GetClass().

    and i tried using UnityEditor.Compilation.AssemblyBiuler.Build method. it also not worked as i want.

    but, i remember it worked in ver 2019.2.19.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    A method with the DidReloadScripts attribute should get called reliably every time that scripts are reloaded.

    If you're trying to load a script asset right after receiving the callback and failing, you might need to wait until the AssetDatabase has finished refreshing.
    Code (CSharp):
    1. [UnityEditor.Callbacks.DidReloadScripts]
    2. private static void CreateAssetWhenReady()
    3. {
    4.     if(EditorApplication.isCompiling || EditorApplication.isUpdating)
    5.     {
    6.         EditorApplication.delayCall += CreateAssetWhenReady;
    7.         return;
    8.     }
    9.  
    10.     EditorApplication.delayCall += CreateAssetNow;
    11. }
    MonoScript.GetClass() can also sometimes return null. This could happen for example if you have more than one class defined inside the same file, or if the class name doesn't perfectly match the filename.
     
  3. ExcellencyHong

    ExcellencyHong

    Joined:
    Aug 9, 2016
    Posts:
    33

    thanks!!

    but, i resolved myself that problem.

    Code (CSharp):
    1. [InitializeOnLoadMethod]
    2. private static void OnInitialized()
    3. {
    4.     if(EditorPrefs.GetBool("HasNew")
    5.     {
    6.         EditorPrefs.DeleteKey("HasNew");
    7.         AssemblyReloadEvents.afterAssemblyReload += CreatedNew;
    8.     }
    9. }