Automate/scriptig the Sprite Editor atlas

I am in serious need of some input on how to handle sprite atlases that has already been made with its sprite definitions in separate script/text files. I have to use them as is, so there is no way of letting Unity create new atlases from sequences of individual sprites from scratch.

Right now I am looking into doing a OnPreprocessTexture and poking the Sprite by this piece of code:

using UnityEngine;
using UnityEditor;
using System.Collections;

class CustomSpriteAtlases : AssetPostprocessor
{
    void OnPreprocessTexture()
    {
        if (assetPath.StartsWith("Assets/SpriteSheets"))
        {
            TextureImporter importer = (TextureImporter)assetImporter;
            importer.textureType = TextureImporterType.Sprite;
            importer.spritePackingTag = "testtag";
            importer.spriteImportMode = SpriteImportMode.Multiple;
            importer.spritePixelsToUnits = 100;

            SpriteMetaData meta = new SpriteMetaData();
            meta.alignment = (int)SpriteAlignment.Center;
            meta.name = "testsprite";
            meta.pivot = new Vector2(0, 0);
            meta.rect = new Rect(0, 0, 100, 100);

            importer.spritesheet = new SpriteMetaData[3];
            importer.spritesheet[0] = meta;
            importer.spritesheet[1] = meta;
            importer.spritesheet[2] = meta;
        }
    }
}

… but so far there is no atlas in sight when doing this. Based on the size of the SpriteMetaData array I do get the correct amount of “atlas sprite definition icons” inside the Sprite texture in the Project view. See attachment.

I refuse to hand place the rects for hundreds of sprite atlases :slight_smile:

			SpriteMetaData[] newMetaData = new SpriteMetaData[3];
			newMetaData[0] = meta;
			newMetaData[1] = meta;
			newMetaData[2] = meta;
			importer.spritesheet = newMetaData;

You can’t modify a struct like that. Change the last part to this and it works. More info about it here:

http://answers.unity3d.com/questions/425510/cant-modify-struct-variables-c.html

Ah, thanks! The error was between the keyboard and chair.