Search Unity

How do I slice a sprite at runtime?

Discussion in '2D' started by Jay1118, Jul 25, 2014.

  1. Jay1118

    Jay1118

    Joined:
    Oct 2, 2012
    Posts:
    9
    I have a Sprite I made at run time and I need to slice it but I can’t find a way to do so at run time and it has to be at runtime for what I am trying to do. Can someone please help me or point me in the right direction.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,435
  3. Jay1118

    Jay1118

    Joined:
    Oct 2, 2012
    Posts:
    9
    Nice but not what I mean, what I mean is to have a sprite root and slice it so it has more sprite child's in the project view to make my own atlas. And just in case I cant use the sprite packer since that makes the build with all of the images and I just want one image since in what I am doing every byte less in the built the better. I am sorry for the misunderstanding I should have been more specific.
     
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Check out the free texture packer importer in store.
    https://www.assetstore.unity3d.com/en/#!/content/16641
    does exactly what you want i think

    Code (CSharp):
    1. /*
    2. *  TexturePacker Importer
    3. *  (c) CodeAndWeb GmbH, Saalbaustraße 61, 89233 Neu-Ulm, Germany
    4. *
    5. *  Use this script to import sprite sheets generated with TexturePacker.
    6. *  For more information see http://www.codeandweb.com/texturepacker/unity
    7. *
    8. *  Thanks to Brendan Campbell for providing a first version of this script!
    9. *
    10. */
    11.  
    12. using UnityEditor;
    13. using System.IO;
    14. using System.Collections;
    15. using System.Collections.Generic;
    16. using UnityEngine;
    17.  
    18. public class TexturePackerImporter : AssetPostprocessor
    19. {
    20.  
    21.     static string[] textureExtensions = {
    22.         ".png",
    23.         ".jpg",
    24.         ".jpeg",
    25.         ".tiff",
    26.         ".tga",
    27.         ".bmp"
    28.     };
    29.  
    30.     /*
    31.      *  Trigger a texture file re-import each time the .tpsheet file changes (or is manually re-imported)
    32.      */
    33.     static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    34.     {
    35.         foreach (var asset in importedAssets) {
    36.             if (!Path.GetExtension (asset).Equals (".tpsheet"))
    37.                 continue;
    38.             foreach (string ext in textureExtensions) {
    39.                 string pathToTexture = Path.ChangeExtension (asset, ext);
    40.                 if (File.Exists (pathToTexture)) {
    41.                     AssetDatabase.ImportAsset (pathToTexture, ImportAssetOptions.ForceUpdate);
    42.                     break;
    43.                 }
    44.             }
    45.         }
    46.     }
    47.  
    48.  
    49.     /*
    50.      *  Trigger a sprite sheet update each time the texture file changes (or is manually re-imported)
    51.      */
    52.     void OnPreprocessTexture ()
    53.     {
    54.         TextureImporter importer = assetImporter as TextureImporter;
    55.  
    56.         string pathToData = Path.ChangeExtension (assetPath, ".tpsheet");
    57.         if (File.Exists (pathToData)) {
    58.             updateSpriteMetaData (importer, pathToData);
    59.         }
    60.     }
    61.  
    62.     static void updateSpriteMetaData (TextureImporter importer, string pathToData)
    63.     {
    64.         importer.textureType = TextureImporterType.Sprite;
    65.         importer.maxTextureSize = 4096;
    66.         importer.spriteImportMode = SpriteImportMode.Multiple;
    67.  
    68.         List<SpriteMetaData> metaData = new List<SpriteMetaData> ();
    69.         foreach (string row in File.ReadAllLines(pathToData)) {
    70.             if (string.IsNullOrEmpty (row) || row.StartsWith ("#"))
    71.                 continue; // comment lines start with #
    72.  
    73.             string [] cols = row.Split (';');
    74.             if (cols.Length != 7)
    75.                 return; // format error
    76.  
    77.             SpriteMetaData smd = new SpriteMetaData ();
    78.             smd.name = cols [0].Replace ("/", "-");  // unity has problems with "/" in sprite names...
    79.             float x = float.Parse (cols [1]);
    80.             float y = float.Parse (cols [2]);
    81.             float w = float.Parse (cols [3]);
    82.             float h = float.Parse (cols [4]);
    83.             float px = float.Parse (cols [5]);
    84.             float py = float.Parse (cols [6]);
    85.  
    86.             smd.rect = new UnityEngine.Rect (x, y, w, h);
    87.             smd.pivot = new UnityEngine.Vector2 (px, py);
    88.  
    89.             if (px == 0 && py == 0)
    90.                 smd.alignment = (int)UnityEngine.SpriteAlignment.BottomLeft;
    91.             else if (px == 0.5 && py == 0)
    92.                 smd.alignment = (int)UnityEngine.SpriteAlignment.BottomCenter;
    93.             else if (px == 1 && py == 0)
    94.                 smd.alignment = (int)UnityEngine.SpriteAlignment.BottomRight;
    95.             else if (px == 0 && py == 0.5)
    96.                 smd.alignment = (int)UnityEngine.SpriteAlignment.LeftCenter;
    97.             else if (px == 0.5 && py == 0.5)
    98.                 smd.alignment = (int)UnityEngine.SpriteAlignment.Center;
    99.             else if (px == 1 && py == 0.5)
    100.                 smd.alignment = (int)UnityEngine.SpriteAlignment.RightCenter;
    101.             else if (px == 0 && py == 1)
    102.                 smd.alignment = (int)UnityEngine.SpriteAlignment.TopLeft;
    103.             else if (px == 0.5 && py == 1)
    104.                 smd.alignment = (int)UnityEngine.SpriteAlignment.TopCenter;
    105.             else if (px == 1 && py == 1)
    106.                 smd.alignment = (int)UnityEngine.SpriteAlignment.TopRight;
    107.             else
    108.                 smd.alignment = (int)UnityEngine.SpriteAlignment.Custom;
    109.  
    110.             metaData.Add (smd);
    111.         }
    112.  
    113.         if (metaData.Count == 0)
    114.             return;
    115.  
    116.         if (importer.spritesheet == null || importer.spritesheet.Length == 0) {
    117.             // replace a blank spritesheet
    118.             importer.spritesheet = metaData.ToArray ();
    119.         } else {
    120.             // merge into an existing spritesheet
    121.             Dictionary<string,SpriteMetaData> importedSprites = new Dictionary<string, SpriteMetaData> ();
    122.             foreach (var smd in metaData) {
    123.                 importedSprites [smd.name] = smd;
    124.             }
    125.  
    126.             // track what has been merged
    127.             HashSet<string> merged = new HashSet<string> ();
    128.  
    129.             List<SpriteMetaData> existingSprites = new List<SpriteMetaData> (importer.spritesheet);
    130.  
    131.             // default struct to replace deleted sprites
    132.             SpriteMetaData deletedSMD = new SpriteMetaData ();
    133.             deletedSMD.rect = new UnityEngine.Rect (0, 0, 1, 1);
    134.  
    135.             // maintain indices
    136.             for (int i = 0; i < existingSprites.Count; ++i) {
    137.                 string oldNameWithoutSlashes = existingSprites [i].name.Replace ("/", "-");
    138.                 if (importedSprites.ContainsKey (existingSprites [i].name) ||
    139.                     importedSprites.ContainsKey (oldNameWithoutSlashes)) {
    140.                     existingSprites [i] = importedSprites [oldNameWithoutSlashes];
    141.                     merged.Add (existingSprites [i].name);
    142.                 } else if (existingSprites [i].name.StartsWith ("DELETED_")) {
    143.                     string origName = existingSprites [i].name.Remove (0, 8);
    144.                     if (importedSprites.ContainsKey (origName)) {
    145.                         existingSprites [i] = importedSprites [origName];
    146.                         merged.Add (origName);
    147.                     }
    148.                 } else {
    149.                     deletedSMD.name = "DELETED_" + existingSprites [i].name;
    150.                     existingSprites [i] = deletedSMD;
    151.                 }
    152.             }
    153.  
    154.             // anything not merged is appended
    155.             foreach (var smd in metaData) {
    156.                 if (!merged.Contains (smd.name)) {
    157.                     existingSprites.Add (smd);
    158.                 }
    159.             }
    160.             importer.spritesheet = existingSprites.ToArray ();
    161.         }
    162.     }
    163. }
    164.