Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. We're excited to be bringing you the newest Unity features, news, demos, and we've even got some special guests from some of the industry’s most storied franchises. Set a reminder to tune in on YouTube here
    Dismiss Notice
  3. Unity 2018.3 is now released.
    Dismiss Notice

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:
    135
    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:
    135
  3. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    120
    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:
    135
  5. Alex_May

    Alex_May

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

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    120
    @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:
    135
    <3 thank you!