Search Unity

AssetDatabase.LoadAllAssetsAtPath doesn't return results during OnPostprocessSprites

Discussion in 'Asset Database' started by MacroPinch, Apr 29, 2020.

  1. MacroPinch

    MacroPinch

    Joined:
    Dec 6, 2013
    Posts:
    43
    Hi, I'm struggling to make an old package work with AssetDatabase V2. The package is SpriteSharp from the AssetStore.

    The problem is that the behavior of the AssetDatabase methods has changed in V2. The plugin works fine in Unity 2019.3.11 with AssetDatabase V1.

    so here's the code:

    using UnityEngine;
    using UnityEditor;

    namespace Test {
    class TestClass : AssetPostprocessor {

    private void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
    {
    var assets = AssetDatabase.LoadAllAssetsAtPath(assetImporter.assetPath);
    Debug.Log("Loaded Assets: " + assets.Length);
    }
    }
    }

    create a texture with multiple sprites and try to reimport it.

    With AssetDatabase V2 you will see Loaded Assets: 0. It will be > 0 with AssetDatabase V1

    Is this a bug or intended behavior?
     
    zzimagination and Klausology like this.
  2. MungeParty

    MungeParty

    Joined:
    Sep 13, 2012
    Posts:
    2
    Future time travelers: you don't need to reimport the sprites, but you do need to wait until after the import to e.g. assign sprite references to custom scriptableobject assets. To do this, just call an async function that does what you need directly from PostProcessSprites and await Task.Delay(1) to resume on the next frame. You can load all asset representations from the texture asset path at that point from the AssetDatabase, including the new sprites.
     
    Antares likes this.