Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Automate/scriptig the Sprite Editor atlas

Discussion in 'Editor & General Support' started by MentalFish, Dec 17, 2013.

  1. MentalFish

    MentalFish

    Joined:
    Nov 2, 2005
    Posts:
    282
    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:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. class CustomSpriteAtlases : AssetPostprocessor
    6. {
    7.     void OnPreprocessTexture()
    8.     {
    9.         if (assetPath.StartsWith("Assets/SpriteSheets"))
    10.         {
    11.             TextureImporter importer = (TextureImporter)assetImporter;
    12.             importer.textureType = TextureImporterType.Sprite;
    13.             importer.spritePackingTag = "testtag";
    14.             importer.spriteImportMode = SpriteImportMode.Multiple;
    15.             importer.spritePixelsToUnits = 100;
    16.  
    17.             SpriteMetaData meta = new SpriteMetaData();
    18.             meta.alignment = (int)SpriteAlignment.Center;
    19.             meta.name = "testsprite";
    20.             meta.pivot = new Vector2(0, 0);
    21.             meta.rect = new Rect(0, 0, 100, 100);
    22.  
    23.             importer.spritesheet = new SpriteMetaData[3];
    24.             importer.spritesheet[0] = meta;
    25.             importer.spritesheet[1] = meta;
    26.             importer.spritesheet[2] = meta;
    27.         }
    28.     }
    29. }
    ... 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 :)
     

    Attached Files:

  2. keely

    keely

    Joined:
    Sep 9, 2010
    Posts:
    967
    Code (csharp):
    1.  
    2.             SpriteMetaData[] newMetaData = new SpriteMetaData[3];
    3.             newMetaData[0] = meta;
    4.             newMetaData[1] = meta;
    5.             newMetaData[2] = meta;
    6.             importer.spritesheet = newMetaData;
    7.  
    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
     
  3. MentalFish

    MentalFish

    Joined:
    Nov 2, 2005
    Posts:
    282
    Ah, thanks! The error was between the keyboard and chair.