Search Unity

[InitializeOnLoad] called during asset import since 2018?

Discussion in 'Immediate Mode GUI (IMGUI)' started by LazloBonin, Jul 12, 2018.

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    I'm investigating a lot of different user reported issues for my plugin Bolt that seem to happen during import and appear to start after Unity 2018.x.

    I do my initialization code uising [InitializeOnLoad] attributes. From my understanding, this attribute calls static type initializers on assembly reload, that is:
    • When the editor starts
    • When entering play mode
    • When the project recompiles
    • After an import of new DLLs
    I think all of my recent issues are coming from that last item. From the symptoms, it seems that importing multiple DLLs causes multiple assembly reloads after each DLL instead of a single assembly reload at the end.

    This is a breaking change for my plugin (and I'm guessing many others!) because I can't guarantee that the my initialization code will work unless all assemblies are properly loaded.

    So my questions are:
    • Was this change in the behaviour of assembly reload documented anywhere?
    • Is there a way of detecting that the Unity editor is currently importing assets?
    If I could answer that second one, I could just skip initialization during asset import and wait for an actual full reload at the end.
     
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
  3. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    Ah! This does seem to do the trick, but it also returns true after all DLLs have been loaded (or when the last DLL is loaded). So if I skip my initialization while isUpdating returns true, it won't initialize after a recompile or an import. :/
     
  4. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    I take that back. EditorApplication.isUpdating sometimes returns false during asset import when opening the project, so it's entirely unreliable for me at the moment.

    Was there any change in the import/assembly load behaviour since 2018? A lot of my users are reporting issues that seem to stem from there.