Author Topic: How to detect if a sprite in an atlas is used.  (Read 4744 times)

Clovos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
How to detect if a sprite in an atlas is used.
« on: December 01, 2016, 09:05:04 AM »
Hey! Is there a way to track if a sprite inside a atlas is used anywhere?
I am in a situation where I know that our biggest atlas contain many sprites that I don't think is used in the game. But our Project is very big so I can't be 100% sure.

Our in house dependency tool does not help me in this case. So I might need to create one myself if necessary.


Cheers!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to detect if a sprite in an atlas is used.
« Reply #1 on: December 02, 2016, 01:24:41 AM »
Unfortunately there is nothing built-in that would do this.

dandrea

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 2
  • Posts: 23
    • View Profile
Re: How to detect if a sprite in an atlas is used.
« Reply #2 on: December 15, 2016, 08:23:31 AM »
Actually, I wrote a tool to tackle this myself. Feel free to use it at your own risk.
Usage: drop the behavior on a root to all the known sprites, fill in the correct slots and use the behavior's context menu. You'll have a list of used (and how much) or unused sprites.
Remember to drag the prefabs to a root as well, or you might end up thinking that a sprite is unused because it's not on the scene, but it's in a prefab.

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. [System.Serializable]
  7. public class SpriteUsage {
  8.   public string spriteName;
  9.   public int count;
  10.   public List<UISprite> sprites;
  11. }
  12.  
  13. public class CheckAtlasUsage : MonoBehaviour {
  14.   public bool m_includeInactive = true;
  15.   public UIAtlas atlas;
  16.   UISprite[] sprites;
  17.   public List<SpriteUsage> usageList;
  18.  
  19.   [ContextMenu("Clear")]
  20.   public void ClearSprites() {
  21.     usageList = new List<SpriteUsage>();
  22.   }
  23.  
  24.   [ContextMenu("Collect sprite usage")]
  25.   public void CollectSprites() {
  26.  
  27.     sprites = this.gameObject.GetComponentsInChildren<UISprite>(m_includeInactive);
  28.     SpriteUsage item;
  29.     foreach (UISpriteData sd in atlas.spriteList) {
  30.  
  31.       item = new SpriteUsage() {
  32.         spriteName = sd.name,
  33.         count = 0,
  34.         sprites = new List<UISprite>()
  35.       };
  36.       foreach (UISprite s in sprites) {
  37.  
  38.         if (s.spriteName == item.spriteName) {
  39.           item.count++;
  40.           item.sprites.Add(s);
  41.         }
  42.       }
  43.       usageList.Add(item);
  44.     }
  45.   }
  46.  
  47.   [ContextMenu("Collect only used sprites")]
  48.   public void CollectSpritesUsed() {
  49.  
  50.     sprites = this.gameObject.GetComponentsInChildren<UISprite>(m_includeInactive);
  51.     SpriteUsage item;
  52.     foreach (UISpriteData sd in atlas.spriteList) {
  53.  
  54.       item = new SpriteUsage() {
  55.         spriteName = sd.name,
  56.         count = 0,
  57.         sprites = new List<UISprite>()
  58.       };
  59.       foreach (UISprite s in sprites) {
  60.  
  61.         if (s.spriteName == item.spriteName) {
  62.           item.count++;
  63.           item.sprites.Add(s);
  64.         }
  65.       }
  66.       if (item.count > 0)
  67.         usageList.Add(item);
  68.       else
  69.         item = null;
  70.     }
  71.   }
  72.  
  73.   [ContextMenu("Collect ununsed sprites")]
  74.   public void CollectUnusedSprites() {
  75.  
  76.     sprites = this.gameObject.GetComponentsInChildren<UISprite>(m_includeInactive);
  77.     SpriteUsage item;
  78.     foreach (UISpriteData sd in atlas.spriteList) {
  79.  
  80.       item = new SpriteUsage() {spriteName = sd.name, count = 0};
  81.       foreach (UISprite s in sprites)
  82.         if (s.spriteName == item.spriteName) item.count++;
  83.       if (item.count == 0) usageList.Add (item);
  84.     }
  85.   }
  86. }
  87.