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

Unity 4.5 Sprite Packer does not pack images inside Resources folder

Discussion in 'Editor & General Support' started by iPicnic, May 27, 2014.

  1. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Thanks Aithoneku.

    For us I don't think there's a different solution than loading each of the hundreds of sprites our game will have dynamically.

    Here's a basic script I've put together for anyone who would like to use a solution similar to what you proposed.

    This scans all sprites inside the Assets/Sprites path, and makes sure there's an equivalent Assets/Resources/_Sprites prefab that points at that Sprite.

    If a sprite is deleted from /Assets/Sprites then the corresponding prefab is deleted.

    It'll run automatically when you press play in unity, or when something changes, so no one needs to worry about keeping that structure up to date....

    If anyone finds a better solution or improves on the following, let me know... Good luck

    Code (CSharp):
    1.  
    2. // C# editor script
    3.  
    4. using UnityEngine;
    5. using UnityEditor;
    6. using System.IO;
    7. using System.Linq;
    8. using System;
    9.  
    10. [InitializeOnLoad]
    11. static class SpriteTreeUpdater
    12. {
    13.     static SpriteTreeUpdater()
    14.     {
    15.         // Run each time the game starts on Editor
    16.         // so no one needs to worry about keeping this structure up to date manually
    17.         UpdateMirrorStructure();
    18.     }
    19.  
    20.     [MenuItem ("Edit/Update Sprite Tree #v")] // CMD + SHIFT + C
    21.     static void UpdateMirrorStructure()
    22.     {
    23.         string sourcePath = Application.dataPath + "/Sprites";
    24.         string destPath = Application.dataPath + "/Resources/_Sprites";
    25.  
    26.         string[] sourceSprites = Directory.GetFiles(sourcePath, "*.png",
    27.                 SearchOption.AllDirectories).Select(x => Path.GetFullPath(x).Substring(sourcePath.Length)).ToArray();
    28.  
    29.         string[] destPrefabs = Directory.GetFiles(destPath, "*.prefab",
    30.                 SearchOption.AllDirectories).Select(x => Path.GetFullPath(x).Substring(destPath.Length)).ToArray();
    31.  
    32.         Array.Sort(sourceSprites); // Alphabetically...
    33.         Array.Sort(destPrefabs);
    34.  
    35.         // Create any new prefabs
    36.         for (int i = 0; i < sourceSprites.Length; ++i)
    37.         {
    38.             string prefabName = sourceSprites[i].Substring(0, sourceSprites[i].Length - 4) + ".prefab";
    39.             int index = Array.IndexOf(destPrefabs, prefabName);
    40.             if (index >= 0)
    41.             {
    42.                 // Prefab exists, nothing to do
    43.                 continue;
    44.             }
    45.  
    46.             // Load sprite
    47.             string spritePath = "Assets/Sprites" + sourceSprites[i].Substring(0, sourceSprites[i].Length);
    48.             Sprite sprite = (Sprite)AssetDatabase.LoadAssetAtPath(spritePath, typeof(Sprite));
    49.             if (sprite == null)
    50.             {
    51.                 Debug.LogError("Unable to load " + spritePath);
    52.                 continue;
    53.             }
    54.  
    55.             // Create GameObject referencing sprite
    56.             GameObject go = new GameObject();
    57.             SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
    58.             sr.sprite = sprite;
    59.  
    60.             // Create directory if it doesn't exist
    61.             string directory = Application.dataPath + "/Resources/_Sprites" + prefabName.Substring(0, prefabName.LastIndexOf('/'));
    62.             System.IO.Directory.CreateDirectory(directory);
    63.  
    64.             // Create the prefab here
    65.             string prefabLocalPath = "Assets/Resources/_Sprites" + prefabName;
    66.             UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab(prefabLocalPath);
    67.             PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);
    68.  
    69.             // Cleanup
    70.             GameObject.DestroyImmediate(go);
    71.         }
    72.  
    73.         // Delete no longer needed prefabs
    74.         for (int i = 0; i < destPrefabs.Length; ++i)
    75.         {
    76.             string spriteName = destPrefabs[i].Substring(0, destPrefabs[i].Length - ".prefab".Length) + ".png";
    77.             int index = Array.IndexOf(sourceSprites, spriteName);
    78.             if (index >= 0)
    79.             {
    80.                 // Sprite exists, nothing to do
    81.                 continue;
    82.             }
    83.  
    84.             string fullPrefabPath = Application.dataPath + "/Resources/_Sprites" + destPrefabs[i];
    85.             System.IO.File.Delete(fullPrefabPath);
    86.             System.IO.File.Delete(fullPrefabPath.Substring(0,fullPrefabPath.Length - "prefab".Length) + ".meta");
    87.  
    88.             Debug.LogWarning("Deleted mirror prefab " + fullPrefabPath);
    89.  
    90.             AssetDatabase.Refresh();
    91.         }
    92.     }
    93. }
    94.  
     
  2. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Quick question: If you have a bunch of sprites with a packing tag that aren't actually used in the game, will these be removed from the sprite atlas when you make a build?

    Thanks
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    If I understand the system correctly, the sprites will be compiled into the sprite atlas no matter what, but that atlas will only be included if it's referenced. So if there's one sprite in the sprite sheet that's used in the game, every sprite in that same sprite sheet will get included.
     
  4. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    ok good to know. Thanks.