How to use SpriteAtlas.Add? SpriteAtlasExtensions.Add?

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

@Venkify ?

Taking a quick look, it could be because the Object is null. Also AssetDatabase requires relative path. Try:

        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++)
            {
                string relativePath = files[j].Replace('\\', '/');
                if (relativePath.StartsWith(Application.dataPath))
                    relativePath = "Assets" + relativePath.Substring(Application.dataPath.Length);

                Object texture = AssetDatabase.LoadMainAssetAtPath(relativePath);
                if (!packables.Contains(texture) && texture != null) textures.Add(texture);
            }
        }

Let me know if this fixes the issue.

1 Like

Coolio, thanks so much for checking it out - I submitted a bug report on this if you want to take a look at it in a project context https://fogbugz.unity3d.com/default.asp?1089125_psins5bs684fpgjs

That worked @Venkify ! Thank you very much. So probably the sprite atlas crashes Unity if passed null to Add().

(I couldn’t debug this because of this problem: VS2015 - "The breakpoint will not currently be hit. Unable to find corresponding location" - Unity Engine - Unity Discussions)

@Alex_May The crash has been fixed if passed NULL to Add and should be available in 2019.1.0a10

1 Like

<3 thank you!