Search Unity

Official Deprecating Sprite Packer Legacy

Discussion in '2D' started by rustum, Feb 24, 2020.

  1. rustum

    rustum

    Unity Technologies

    Joined:
    Feb 14, 2015
    Posts:
    190
    We are in the process of deprecating Sprite Packer Legacy(SpriteAtlas packing through option of PackingTags in the TextureImporter settings). However we are still allowing existing Assets to continue working with the following constraints :
    1. The Packing Tags box in Texture Importer is now read-only.
    2. Atlases based on Packing Tags are not automatically packed. Requires calling the script using IPreprocessBuildWithReport
    3. Editor Packer Settings only shows Disabled and SpriteAtlas options
    We strongly recommend that you start using the SpriteAtlas system which has better functionality, however if for some reason you wish to continue using PackingTags you may need to add the following Editor script.

    Editor Settings no longer displays Legacy Sprite Packer options:
    Editor Settings.png

    Custom Script to force Packing Atlas for Packing Tags:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEditor.Build;
    5. using UnityEditor.Build.Reporting;
    6. using UnityEngine;
    7. class BuildPreProcessor : IPreprocessBuildWithReport
    8. {
    9.     public int callbackOrder { get { return 0; } }
    10.     public void OnPreprocessBuild(BuildReport report)
    11.     {
    12.         EditorSettings.spritePackerMode = SpritePackerMode.BuildTimeOnly;
    13. UnityEditor.Sprites.Packer.RebuildAtlasCacheIfNeeded(BuildTarget.StandaloneWindows64, false);
    14.     }
    15. }
    16.  

    Note:
    BuildPreProcessor
    class inherits from
    IPreprocessBuildWithReport
    , thus it only triggers when building


    To migrate existing tag based Atlas to SpriteAtlas, please use the following conversion Script:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEditor.U2D;
    6. using UnityEngine.U2D;
    7.  
    8. public class PrintAtlas : MonoBehaviour
    9. {
    10.     [MenuItem("Atlas/Print Usage")]
    11.     static void AtlasedSprite()
    12.     {
    13.         Dictionary<string, List<string>> TagSpriteMap = new Dictionary<string, List<string>>();
    14.  
    15.         foreach (string s in AssetDatabase.GetAllAssetPaths())
    16.         {
    17.             TextureImporter ta = AssetImporter.GetAtPath(s) as TextureImporter;
    18.             if (ta != null)
    19.             {
    20.                 if (ta.spritePackingTag != "")
    21.                 {
    22.                     if (!TagSpriteMap.ContainsKey(ta.spritePackingTag))
    23.                     {
    24.                         List<string> newList = new List<string>();
    25.                         TagSpriteMap[ta.spritePackingTag] = newList;
    26.                     }
    27.                     TagSpriteMap[ta.spritePackingTag].Add(s);
    28.                 }
    29.             }
    30.         }
    31.  
    32.         AssetDatabase.CreateFolder("Assets", "SpriteAtlas");
    33.         foreach (var tagSprite in TagSpriteMap)
    34.         {
    35.             List<string> assetsforTag = tagSprite.Value;
    36.             string alasPath = "Assets/SpriteAtlas/" + tagSprite.Key + ".spriteatlas";
    37.             SpriteAtlas sa = new SpriteAtlas();
    38.             AssetDatabase.CreateAsset(sa, alasPath);
    39.  
    40.             foreach (var asset in assetsforTag)
    41.             {
    42.                 Debug.Log(tagSprite.Key + " = " + asset);
    43.                 Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(asset);
    44.                 sa.Add(new Object[1] { tex } );
    45.             }
    46.         }
    47.  
    48.         AssetDatabase.SaveAssets();
    49.     }
    50. }
    51.  
     
    Last edited: Feb 24, 2020
    FoxFisher, _creatio_, Ferazel and 3 others like this.
  2. ChaosHelme

    ChaosHelme

    Joined:
    Nov 22, 2016
    Posts:
    4
    Thank's a lot for this post, I was looking for a solution for a while now xD
     
    rustum likes this.
  3. AndyBlock

    AndyBlock

    Joined:
    Oct 12, 2016
    Posts:
    36
    Something to watch out for; so far as I can tell, this script ignores the texture import settings (e.g. compression format, pixel format etc). Not only will the final atlas have different settings from the original sprites, but if the original sprites had different settings, they will still all get lumped into the same atlas, where the old sprite packer would end up making several separate atlases for them. Still, this is a useful start, so thanks! But if anyone knows of a solution that will take the settings into account that would be great!
     
  4. travelsound84

    travelsound84

    Joined:
    Jan 15, 2014
    Posts:
    34
    @rustum, we had to upgrade our unity to 2020, so now as you explained, the Packing Tag is disabled and I had to reorganize our Sprites. I have a few questions about it:
    1. If I now put one of my sprites into a SpriteAtlas, it still has the Packing Tag on it. Is that something I need to worry about?
    2. What about the settings on each sprite. Are they going to be overwritten by the settings on the SpriteAtlas? I'm talking about "Filter mode", "Compression" and other properties that I find in both places.

    Thanks for clarifying!
     

    Attached Files:

    rustum likes this.
  5. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Hi @travelsound84

    1. If I now put one of my sprites into a SpriteAtlas, it still has the Packing Tag on it. Is that something I need to worry about?
    No, once you migrated to Atlas (either using the scripts above) or manually please ensure that SpriteAtlas is set in Editor Settings -> SpritePacker Mode. PackingTag will be ignore while building SpriteAtlas Textures.
    2. What about the settings on each sprite. Are they going to be overwritten by the settings on the SpriteAtlas? I'm talking about "Filter mode", "Compression" and other properties that I find in both places.
    Legacy SpritePacker groups sprites based on PackerPolicy (configurable through Scripting Unity - Manual: Sprite Packer (unity3d.com) ). These settings are not migrated to SpriteAtlas automatically as each SpriteAtlas provides the ability to customize these settings per Atlas. Hence please ensure that you set these properties for each Atlas either through Inspector or through Scripting.

    Please take a look at Unity - Manual: Sprite Atlas workflow (unity3d.com). Please do let us know if you need more info.
     
    rustum likes this.
  6. travelsound84

    travelsound84

    Joined:
    Jan 15, 2014
    Posts:
    34
    @Venkify, thanks for the information.
    I think the new workflow makes more sense. Will let you know if I find something odd.

    Cheers!
     
  7. nindim

    nindim

    Joined:
    Jan 22, 2013
    Posts:
    130
    @rustum @travelsound84 @Venkify

    Sorry to necro this thread, but this is high on google for this topic and formed the basis of my migration a while back.

    Leaving the legacy sprite packing tag set prevents textures that are subsequently made into loose leaf textures from being compressed at all, they should definitely be removed.

    I have seen a few instances of this in my project now, some textures that should have been about 1.2MB were 10.5MB. Other textures were ~124KB when they should have been ~14KB.

    Notice the suffix "(Atlas)", this means a legacy packing tag is set and your texture will not be compressed:

    upload_2023-2-12_8-57-19.png

    Once the tag is removed the texture looks like this:

    upload_2023-2-12_8-59-0.png

    Here is a script to strip out all the legacy tags, this will not migrate you to the newer system so use with caution. Make sure you back things up too! :)

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class SpriteAtlasTools : MonoBehaviour
    5. {
    6.     [MenuItem("Atlas/Strip Legacy Tags")]
    7.     static void StripLegacyTags()
    8.     {
    9.         AssetDatabase.StartAssetEditing();
    10.  
    11.         foreach (string s in AssetDatabase.GetAllAssetPaths())
    12.         {
    13.             TextureImporter ta = AssetImporter.GetAtPath(s) as TextureImporter;
    14.             if (ta != null)
    15.             {
    16.                 if (ta.spritePackingTag != "")
    17.                 {
    18.                     Debug.Log($"Stripping legacy packing tag for: {s}");
    19.                     ta.spritePackingTag = "";
    20.                     ta.SaveAndReimport();
    21.                 }
    22.             }
    23.         }
    24.  
    25.         AssetDatabase.StopAssetEditing();
    26.         AssetDatabase.SaveAssets();
    27.     }
    28. }
     
    CameronND and funitycn like this.