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

How to implement custom sprite packer policy?

Discussion in 'Editor & General Support' started by SoftwareGeezers, Nov 28, 2015.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    My sprites are being packed in 1k textures. Trying to change this, it seems I need to create a custom policy based on this:
    http://docs.unity3d.com/430/Documentation/Manual/SpritePacker.html

    I can't get it to work. It doesn't build and is clearly missing references in that example. But even adding missing references like using Unity.Editor.Sprites and such, there are aspects the compiler doesn't know like TextureImportInstructions()

    Can someone provide a working script showing how to use the packer policy?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Sprites;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. class NewPackerPolicy : IPackerPolicy
    8. {
    9.     private class Entry
    10.     {
    11.         public Sprite         sprite;
    12.         public AtlasSettings  settings;
    13.         public string         tag;
    14.         public SpriteMeshType meshType;
    15.     }
    16.    
    17.     public int GetVersion()
    18.     {
    19.         return 1;
    20.     }
    21.    
    22.     public void OnGroupAtlases(BuildTarget target, PackerJob job, TextureImporter[] textureImporters)
    23.     {
    24.         List<Entry> entries = new List<Entry>();
    25.        
    26.         foreach (TextureImporter ti in textureImporters)
    27.         {
    28.             TextureImportInstructions ins = new TextureImportInstructions();
    29.             ti.ReadTextureImportInstructions(ins, target);
    30.            
    31.             TextureImporterSettings tis = new TextureImporterSettings();
    32.             ti.ReadTextureSettings(tis);
    33.            
    34.             Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray();
    35.             foreach (Sprite sprite in sprites)
    36.             {
    37.                 Entry entry = new Entry();
    38.                 entry.sprite = sprite;
    39.                 entry.settings.format = ins.desiredFormat;
    40.                 entry.settings.usageMode = ins.usageMode;
    41.                 entry.settings.colorSpace = ins.colorSpace;
    42.                 entry.settings.compressionQuality = ins.compressionQuality;
    43.                 entry.settings.filterMode = ti.filterMode;
    44.                 entry.settings.maxWidth = 2048;
    45.                 entry.settings.maxHeight = 2048;
    46.                 entry.tag = ti.spritePackingTag;
    47.                 entry.meshType = tis.spriteMeshType;
    48.                
    49.                 entries.Add(entry);
    50.             }
    51.         }
    52.        
    53.         // First split sprites into groups based on packing tag
    54.         var tagGroups =
    55.             from e in entries
    56.                 group e by e.tag;
    57.         foreach (var tagGroup in tagGroups)
    58.         {
    59.             int page = 0;
    60.             // Then split those groups into smaller groups based on texture settings
    61.             var settingsGroups =
    62.                 from t in tagGroup
    63.                     group t by t.settings;
    64.             foreach (var settingsGroup in settingsGroups)
    65.             {
    66.                 string atlasName = string.Format("{0}", tagGroup.Key);
    67.                 if (settingsGroups.Count() > 1)
    68.                     atlasName += string.Format(" (Group {0})", page);
    69.                
    70.                 job.AddAtlas(atlasName, settingsGroup.Key);
    71.                 foreach (Entry entry in settingsGroup)
    72.                 {
    73.                     SpritePackingMode packingMode = (entry.meshType == SpriteMeshType.Tight) ? SpritePackingMode.Tight : SpritePackingMode.Rectangle;
    74.                     job.AssignToAtlas(atlasName, entry.sprite, packingMode, SpritePackingRotation.None);
    75.                 }
    76.                
    77.                 ++page;
    78.             }
    79.         }
    80.     }
    81. }
     
  2. Zanrok

    Zanrok

    Joined:
    Aug 9, 2012
    Posts:
    2
    Any luck in figuring this out?
     
  3. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902