I’m currently working on custom 2D models. In my workflow, I copy to the Asset directory of my project a directory containing binary files and a bunch of pngs that will be used as sprites. Using the OnPostprocessAllAssets callback on a AssetPostProcessor class, I create an asset for my model and I convert all textures in the folder to Sprites with adapted parameters.
Up to this point, everything work fine, but I also need to reference in my asset all the sprites associated with the model. So I try loading the sprites using AssetDatabase.LoadAllAssetRepresentationsAtPath, but for textures that I just tried to convert to sprite, it returns an empty array :
TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(relativeFile.Path);
TextureImporterSettings importerSettings = new TextureImporterSettings();
textureImporter.ReadTextureSettings(importerSettings);
textureImporter.spritePackingTag = "atlasName";
importerSettings.ApplyTextureType(TextureImporterType.Sprite, true);
importerSettings.spriteMeshType = SpriteMeshType.Tight;
importerSettings.spritePixelsPerUnit = 1;
importerSettings.spriteAlignment = (int)SpriteAlignment.TopLeft;
textureImporter.SetTextureSettings(importerSettings);
AssetDatabase.ImportAsset(relativeFile.Path, ImportAssetOptions.ForceUpdate);
UnityEngine.Object[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(relativeFile.Path); // sprites = []
I tried different options for the ImportAssetOptions parameter, but Unity doesn’t seem to care and my texture is not actually converted to sprite before my importer script returns.
Does anyone know of a workaround ?