Search Unity

How to use SpriteAtlas.Add? SpriteAtlasExtensions.Add?

Discussion in '2D Experimental Preview' started by Alex_May, Oct 13, 2018.

  1. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    147
    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:

    Code (CSharp):
    1.         // get the atlas at the special location
    2.         string AtlasPath = "Assets/Raw Graphic Data/Atlas.spriteatlas";
    3.         SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(AtlasPath);
    4.         if (atlas == null) CreateAtlas(AtlasPath);
    5.  
    6.         // get the packables in the atlas
    7.         List<Object> packables = new List<Object>(SpriteAtlasExtensions.GetPackables(atlas));
    8.  
    9.         // get a list of all textures to pack
    10.         List<Object> textures = new List<Object>();
    11.         string[] folders = AssetDatabase.GetSubFolders("Assets/Raw Graphic Data");
    12.         for (int i = 0; i < folders.Length; i++)
    13.         {
    14.             string folder = folders[i].Replace('\\', '/');
    15.             string path = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + folder;
    16.             string[] files = Directory.GetFiles(path, "*.png");
    17.             for (int j = 0; j < files.Length; j++)
    18.             {
    19.                 Object texture = AssetDatabase.LoadMainAssetAtPath(files[j]);
    20.                 if (!packables.Contains(texture)) textures.Add(texture);
    21.             }
    22.         }
    23.  
    24.         atlas.Add(textures.ToArray()); // crash here
     
  2. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    147
  3. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    154
    Taking a quick look, it could be because the Object is null. Also AssetDatabase requires relative path. Try:

    Code (CSharp):
    1.         for (int i = 0; i < folders.Length; i++)
    2.         {
    3.             string folder = folders[i].Replace('\\', '/');
    4.             string path = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + folder;
    5.             string[] files = Directory.GetFiles(path, "*.png");
    6.             for (int j = 0; j < files.Length; j++)
    7.             {
    8.                 string relativePath = files[j].Replace('\\', '/');
    9.                 if (relativePath.StartsWith(Application.dataPath))
    10.                     relativePath = "Assets" + relativePath.Substring(Application.dataPath.Length);
    11.  
    12.                 Object texture = AssetDatabase.LoadMainAssetAtPath(relativePath);
    13.                 if (!packables.Contains(texture) && texture != null) textures.Add(texture);
    14.             }
    15.         }

    Let me know if this fixes the issue.
     
    Alex_May likes this.
  4. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    147
  5. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    147
  6. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    154
    @Alex_May The crash has been fixed if passed NULL to Add and should be available in 2019.1.0a10
     
    Alex_May likes this.
  7. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    147
    <3 thank you!