Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AssetDatabase.ImportPackage Callbacks don't work ?

Discussion in 'Asset Database' started by baptisteb_unity, Aug 6, 2018.

  1. baptisteb_unity

    baptisteb_unity

    Joined:
    Feb 13, 2018
    Posts:
    4
    Hello !

    I'm making a tool where I have to import a custom package from Github, and I need a call back when it's completed in editor.

    For that I use as recommend by the doc the AssetDatabase.ImportPackage callback, more precisely AssetDatabase.importPackageCompleted.

    Here is the function where I declare the callbacks:

    Code (CSharp):
    1. void Download()
    2. {
    3. AssetDatabase.importPackageCompleted += ImportCompleted;
    4.                             AssetDatabase.importPackageCancelled += ImportCancelled;
    5.                             AssetDatabase.importPackageFailed += ImportCallBackFailed;
    6.                             AssetDatabase.importPackageStarted += ImportStarted;
    7.  
    8. AssetDatabase.ImportPackage(path, true);
    9.  
    10. }
    11.  
    12.  
    And here the functions called by the callbacks:

    Code (CSharp):
    1.  static void ImportCompleted(string packageName)
    2.         {
    3.             Debug.Log ("Completed " + packageName);
    4.             AssetDatabase.importPackageCompleted -= ImportCompleted;
    5.         }
    6.        
    7.         static void ImportCancelled(string packageName)
    8.         {
    9.             Debug.Log ("Cancelled " + packageName);
    10.             AssetDatabase.importPackageCancelled -= ImportCancelled;
    11.         }
    12.        
    13.         static void ImportCallBackFailed(string packageName, string _error)
    14.         {
    15.             Debug.Log ("Failed " + packageName);
    16.             AssetDatabase.importPackageFailed -= ImportCallBackFailed;
    17.         }
    18.        
    19.         static void ImportStarted(string packageName)
    20.         {
    21.             Debug.Log ("Started " + packageName);
    22.             AssetDatabase.importPackageStarted -= ImportStarted;
    23.         }
    The only callback that works is the ImportStarted one.
    Any idea?
     
  2. PsyKaw

    PsyKaw

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

    baptisteb_unity

    Joined:
    Feb 13, 2018
    Posts:
    4
    Hello !
    I try to put an AssetDatabase.Refresh (before and after by the way ^^) but it doesn't work.
     
  4. baptisteb_unity

    baptisteb_unity

    Joined:
    Feb 13, 2018
    Posts:
    4
    Oh btw, the issue tracker is put as fixed but no version is precise.
     
  5. BaptisteB

    BaptisteB

    Joined:
    Mar 11, 2015
    Posts:
    12
  6. BaptisteB

    BaptisteB

    Joined:
    Mar 11, 2015
    Posts:
    12
    Someone?
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,227
    It doesn't say fixed. It says by design which means that's how it's supposed to work.

     
    _geo__ likes this.
  8. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    The AssetDatabase.importPackageCancelled does not reimport or recompile and it's not being called. Is there a specific way to receive those calls? @karl_jones
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,227
    If it's not being called then it sounds like a bug. Can you please send a bug report?
     
  10. BaptisteB

    BaptisteB

    Joined:
    Mar 11, 2015
    Posts:
    12
    Neither the AssetDatabase.importPackageCompleted and AssetDatabase.importCallBackFailed.
    That was part of the original question. Or maybe there is something I don't understand...

    @karl_jones do we need to post three bug report or just 1 ?
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,227
    One bug report is fine.
     
  12. baptisteb_unity

    baptisteb_unity

    Joined:
    Feb 13, 2018
    Posts:
    4
    @Martin_Gonzalez Did you send a bug report? If yes, could you share a link to the issue tracker?
     
  13. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
  14. BaptisteB

    BaptisteB

    Joined:
    Mar 11, 2015
    Posts:
    12
    Did you manage to make the other callbacks (Completed, Started, Failed) work? @Martin_Gonzalez
     
  15. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    I didn't use other evenst, I just wanted that one because my idea was to delete the file after import is completed.
     
  16. Deepscorn

    Deepscorn

    Joined:
    Aug 13, 2015
    Posts:
    25
    As I see others wondering about receiving complete callback, will add some info. I receive complete callback. What I did: added log in static constructor to see when my class, waiting for callback is recreated. It is recreated for example, if assembly in which it resides is being recompiled. It may happen, if you import package which contain code. Note that. To save state between recompiles, you can write to file or use PlayerPrefs. I suggest you start from logging, like I did:

    Code (CSharp):
    1.     [InitializeOnLoad]
    2.     public class PackageRegistrar : AssetPostprocessor
    3.     {
    4.         static PackageRegistrar()
    5.         {
    6.             Debug.Log("PackageRegistrar: s_ctor");
    7.             AssetDatabase.importPackageStarted += OnImportPackageStarted;
    8.             AssetDatabase.importPackageCompleted += OnImportPackageCompleted;
    9.         }
    10.  
    11.         public static void OnPostprocessAllAssets(
    12.             string[] importedAssets
    13.             , string[] deleted
    14.             , string[] movedAssets
    15.             , string[] movedFrom)
    16.         {
    17.             LogIfAny("importedAssets", importedAssets);
    18.             LogIfAny("deleted", deleted);
    19.             LogIfAny("movedAssets", movedAssets);
    20.             LogIfAny("movedFrom", movedFrom);
    21.         }
    22.  
    23.         private static void OnImportPackageStarted(string packagename)
    24.         {
    25.             Debug.Log($"PackageRegistrar: OnImportPackageStarted({packagename})");
    26.         }
    27.      
    28.         private static void OnImportPackageCompleted(string packagename)
    29.         {
    30.             Debug.Log($"PackageRegistrar: OnImportPackageCompleted({packagename})");
    31.         }
    32. }
    The output is:

    PackageRegistrar: PackageRegistrar: OnImportPackageStarted(external-dependency-manager-latest)
    PackageRegistrar: PackageRegistrar: s_ctor // so, it is recompiled in-between
    PackageRegistrar: PackageRegistrar: OnImportPackageCompleted(external-dependency-manager-latest)
     
    Last edited: Mar 27, 2020
    Hayao-Gai and Figo_Hunter like this.
  17. Figo_Hunter

    Figo_Hunter

    Joined:
    Feb 12, 2016
    Posts:
    7
    Thank you. You are right about the recompilation thing that I ignored. So basically if the unitypackage contains any script file or assembly definition that will trigger the recompilation process, the callback function delegate will be cleared. Therefore, the delegate registration needs to take place after the recompiling. What Ive done is to put the delegate registration in a OnInitializeLoad class.

    Just found out you have done the same thing. And it is very nice to use EditorPrefs store the state.