Anyone used these to build SpriteAtlas assets procedurally? I am writing an asset pipeline for sprites and want to pack sprites in atlases after importing them with an AssetPostProcessor.
What’s happening is that when I add the texture assets that contain the sprites to the sprite atlas using SpriteAtlas.Add or SpriteAtlasExtensions.Add, Unity crashes hard.
Version is 2018.2.6f1.
Code looks like this:
// get the atlas at the special location
string AtlasPath = "Assets/Raw Graphic Data/Atlas.spriteatlas";
SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(AtlasPath);
if (atlas == null) CreateAtlas(AtlasPath);
// get the packables in the atlas
List<Object> packables = new List<Object>(SpriteAtlasExtensions.GetPackables(atlas));
// get a list of all textures to pack
List<Object> textures = new List<Object>();
string[] folders = AssetDatabase.GetSubFolders("Assets/Raw Graphic Data");
for (int i = 0; i < folders.Length; i++)
{
string folder = folders[i].Replace('\\', '/');
string path = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + folder;
string[] files = Directory.GetFiles(path, "*.png");
for (int j = 0; j < files.Length; j++)
{
Object texture = AssetDatabase.LoadMainAssetAtPath(files[j]);
if (!packables.Contains(texture)) textures.Add(texture);
}
}
atlas.Add(textures.ToArray()); // crash here