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

TextureImportInstructions?

Discussion in '2D' started by patoffspyder, Apr 3, 2015.

  1. patoffspyder

    patoffspyder

    Joined:
    Jan 22, 2014
    Posts:
    11
    Hi guys,
    I want to create a new Packer Policy to be sure I don't have any atlases bigger than 1024.
    http://docs.unity3d.com/Manual/SpritePacker.html
    In the documentation, there is the code for the DefaultSpritePolicy and there is a part saying :
    TextureImportInstructions ins = new TextureImportInstructions();
    ti.ReadTextureImportInstructions(ins, target);

    I have an error with TextureImportInstructions, it looks like it doesn't exist anymore in Unity 5, is it possible? I can't find anything about the SpritePolicy, it looks like everybody else are just using the Default one.

    Thanks!
     
  2. CoalCzar

    CoalCzar

    Joined:
    Nov 25, 2012
    Posts:
    22
    Ok, for anyone who's still struggling with this, the solution is pretty simple, but as of now undocumented.

    Replace

    Code (CSharp):
    1.      
    2. foreach (int instanceID in textureImporterInstanceIDs)
    3.         {
    4.             TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter;
    5.  
    6.             TextureImportInstructions ins = new TextureImportInstructions();
    7.             ti.ReadTextureImportInstructions(ins, target);
    8.  
    9.             TextureImporterSettings tis = new TextureImporterSettings();
    10.             ti.ReadTextureSettings(tis);
    11.  
    12.             Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray();
    13.             foreach (Sprite sprite in sprites)
    14.             {
    15.                 Entry entry = new Entry();
    16.                 entry.sprite = sprite;
    17.                 entry.settings.format = ins.desiredFormat;
    18.                 entry.settings.usageMode = ins.usageMode;
    19.                 entry.settings.colorSpace = ins.colorSpace;
    20.                 entry.settings.compressionQuality = ins.compressionQuality;
    21.                 entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear;
    22.                 entry.settings.maxWidth = 2048;
    23.                 entry.settings.maxHeight = 2048;
    24.                 entry.settings.generateMipMaps = ti.mipmapEnabled;
    25.                 if (ti.mipmapEnabled)
    26.                     entry.settings.paddingPower = kDefaultPaddingPower;
    27.                 entry.atlasName = ParseAtlasName(ti.spritePackingTag);
    28.                 entry.packingMode = GetPackingMode(ti.spritePackingTag, tis.spriteMeshType);
    29.                 entry.anisoLevel = ti.anisoLevel;
    30.  
    31.                 entries.Add(entry);
    32.             }
    33.  
    34.             Resources.UnloadAsset(ti);
    35.         }
    with

    Code (CSharp):
    1. foreach (int instanceID in textureImporterInstanceIDs)
    2.         {
    3.             TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter;
    4.  
    5.             TextureFormat textureFormat;
    6.             ColorSpace colorSpace;
    7.             int compressionQuality;
    8.             ti.ReadTextureImportInstructions(target, out textureFormat, out colorSpace, out compressionQuality);
    9.          
    10.             TextureImporterSettings tis = new TextureImporterSettings();
    11.             ti.ReadTextureSettings(tis);
    12.          
    13.             Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray();
    14.             foreach (Sprite sprite in sprites)
    15.             {
    16.                 Entry entry = new Entry();
    17.                 entry.sprite = sprite;
    18.                 entry.settings.format = textureFormat;
    19.                 entry.settings.colorSpace = colorSpace;
    20.                 entry.settings.compressionQuality = compressionQuality;
    21.                 entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear;
    22.                 entry.settings.maxWidth = 2048;
    23.                 entry.settings.maxHeight = 2048;
    24.                 entry.settings.generateMipMaps = ti.mipmapEnabled;
    25.                 if (ti.mipmapEnabled)
    26.                     entry.settings.paddingPower = kDefaultPaddingPower;
    27.                 entry.atlasName = ParseAtlasName(ti.spritePackingTag);
    28.                 entry.packingMode = GetPackingMode(ti.spritePackingTag, tis.spriteMeshType);
    29.                 entry.anisoLevel = ti.anisoLevel;
    30.              
    31.                 entries.Add(entry);
    32.             }
    33.          
    34.             Resources.UnloadAsset(ti);
    35.         }
    The ReadTextureImportInstructions method still exists, but doesn't take TextureImportInstructions (as that class no longer exists), so I substituted the target supplied by OnGroupAtlases instead. Also, usageMode is a property that vanished, so I omitted that line. The above seems to work identically to the defaultPackingPolicy when selected in the Sprite Packer window.
     
    Last edited: Apr 14, 2015
    russcat likes this.