Search Unity

Simple Editor/Pack Textures question

Discussion in 'Editor & General Support' started by Shizen, Mar 13, 2018.

  1. Shizen

    Shizen

    Joined:
    Dec 31, 2012
    Posts:
    14
    So I wanted to be able to quickly build small texture atlases for testing some code. I'm pretty ignorant about textures, but I thought this would be easy to do in the editor, so I tried the following...

    Code (csharp):
    1.  
    2. public class TextureAtlasWizard : ScriptableWizard {
    3.     public Texture2D[] textures;
    4.  
    5.     [MenuItem("Assets/Create/Texture Atlas")]
    6.     static void CreateWizard() {
    7.         ScriptableWizard.DisplayWizard<TextureAtlasWizard>("Create Texture Atlas", "Create");
    8.     }
    9.  
    10.     private void OnWizardCreate() {
    11.         if (textures.Length == 0) {
    12.             return;
    13.         }
    14.         string path = EditorUtility.SaveFilePanelInProject("Save Texture Atlas", "Texture Atlas", "png", "Save Texture Atlas");
    15.         if (path.Length == 0) {
    16.             return;
    17.         }
    18.         // Get dimensions
    19.         int count = textures.Length;
    20.         Texture2D t = textures[0];
    21.         int sizeX = t.width * ((count + 3) / 4);
    22.         int sizeY = t.height * 4;
    23.         //Texture2D atlas = new Texture2D(sizeX, sizeY);
    24.         //Texture2D atlas = new Texture2D(0, 0);
    25.         Texture2D atlas = new Texture2D(512, 512);
    26.         Rect[] rects = atlas.PackTextures(textures, 0, 512);
    27.         //atlas.Apply();
    28.         byte[] p = atlas.EncodeToPNG();
    29.         File.WriteAllBytes(path, p);
    30.     }
    31. }
    32.  
    As you might intuit, I played around a bit trying to get PackTextures to work for me. No matter what I tried, I always get an empty texture. The textures show up in `textures`, but they do not get packed into `atlas`. `rects` isn't null, but it is an array of zero'd rects of a size equal to the number of textures I attempted to pack. Anyone know what I'm missing?

    Also, after the function exits I receive a log message...

    Code (csharp):
    1.  
    2. Texture atlas needs textures to have Readable flag set!
    3. UnityEngine.Texture2D:PackTextures(Texture2D[], Int32, Int32)
    4. TextureAtlasWizard:OnWizardCreate() (at Assets/Editor/TextureAtlasWizard.cs:31)
    5. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    6.  
    As far as I can verify in the debugger, the textures are being read...
     
  2. Shizen

    Shizen

    Joined:
    Dec 31, 2012
    Posts:
    14
    All the textures I'm working with are the same size, btw ;)
     
  3. Shizen

    Shizen

    Joined:
    Dec 31, 2012
    Posts:
    14
    Anyone know?
     
  4. TurboHermit

    TurboHermit

    Joined:
    Dec 20, 2011
    Posts:
    40
    Apologies for resurrecting this, but I'm running into the same problem at the moment and can't seem to find any solutions to it yet. Did you ever manage to resolve this issue?

    I've also tried different variants of saving as an asset, making sure the read/write is enabled and loading it afterward, but even then it throws the same error.
     
  5. TurboHermit

    TurboHermit

    Joined:
    Dec 20, 2011
    Posts:
    40
    Jumped the gun on this one... Apparently the source textures you try to pack into the atlas ALSO need to be readable. Since we don't want to enable read/write on the source assets, we create temporary cached texture2D, copy raw data from the assets and feed those to PackTextures instead of the assets.