Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Slicing a Sprite through script on Importing

Discussion in '2D' started by Garmichael, Jun 26, 2019.

  1. Garmichael

    Garmichael

    Joined:
    Mar 17, 2013
    Posts:
    17
    Hey all,

    I've written some script that slices a sprite into equal regions like a grid according to a region Width and Height property. When I updated Unity to 2019.2, this script stopped working properly. This script is accessed in an editor window I built that allows me to choose a png file through a file browser and import it.

    If I import an image that isn't a Power of Two, the image is resized to the nearest Power of Two automatically, and then the slicing gets messed up. For example, If I import an image thats 160x48 and attempt to slice it into chunks of 16x16, I expect 30 slices (10x3). However, the image actually comes in at 128x64 and that results in 24 slices (8x4).

    Here's the script:

    Code (CSharp):
    1. public static void SliceSprite(string texturePath, int sizeX, int sizeY) {
    2.     TextureImporter textureImporter = AssetImporter.GetAtPath(texturePath) as TextureImporter;
    3.  
    4.     if (textureImporter == null) {
    5.         return;
    6.     }
    7.  
    8.     textureImporter.textureType = TextureImporterType.Sprite;
    9.     textureImporter.spriteImportMode = SpriteImportMode.Multiple;
    10.     textureImporter.spritePixelsPerUnit = GameProperties.PixelsPerUnit;
    11.     textureImporter.filterMode = FilterMode.Point;
    12.     textureImporter.wrapMode = TextureWrapMode.Clamp;
    13.     textureImporter.maxTextureSize = 2048;
    14.     textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
    15.     textureImporter.crunchedCompression = false;
    16.     textureImporter.compressionQuality = 100;
    17.     textureImporter.isReadable = true;
    18.     textureImporter.textureShape = TextureImporterShape.Texture2D;
    19.     textureImporter.npotScale = TextureImporterNPOTScale.None;
    20.  
    21.     AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
    22.  
    23.     Texture2D sourceTexture = (Texture2D) AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
    24.  
    25.     Debug.Log(sourceTexture.width);
    26.     Debug.Log(sourceTexture.height);
    27.  
    28.     List<SpriteMetaData> spriteMetaDatas = new List<SpriteMetaData>();
    29.     int frameNumber = 0;
    30.     for (int j = sourceTexture.height; j > 0; j -= sizeY) {
    31.         for (int i = 0; i < sourceTexture.width; i += sizeX) {
    32.             SpriteMetaData spriteMetaData = new SpriteMetaData {
    33.                 name = sourceTexture.name + "_" + frameNumber,
    34.                 rect = new Rect(i, j - sizeY, sizeX, sizeY),
    35.                 alignment = 0,
    36.                 pivot = new Vector2(0f, 0f)
    37.             };
    38.  
    39.             spriteMetaDatas.Add(spriteMetaData);
    40.             frameNumber++;
    41.         }
    42.     }
    43.  
    44.     Debug.Log(spriteMetaDatas.Count);
    45.     textureImporter.spritesheet = spriteMetaDatas.ToArray();
    46.     AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
    47. }
    This code will Log the correct width and height, but won't slice the image. The second Log of spriteMetaDatas.Count will report the correct number of slices.

    If I remove Line 21, the Logs will report the wrong image size (resized to the nearest Power of Two), and the sprite will be sliced but incorrectly.


    How do I fix it so that I can slice with the correct Dimensions?
     
    QuariYune and Holonet like this.
  2. Garmichael

    Garmichael

    Joined:
    Mar 17, 2013
    Posts:
    17
    Thanks to some help on the GDL Discord Unity-Dev channel, I got the solution.

    I added:

    EditorUtility.SetDirty(textureImporter);

    After Line 21 where the Asset was imported with the first set of settings
     
    liuchunyao90, QuariYune and Holonet like this.