LoginSignup
2
3

More than 5 years have passed since last update.

[uGUI]Prefabに余計なSpritePackが入っていないか調べる

Posted at

uGUIではSpriteにPackingTagを設定するだけで自動的にAtlas化してくれてとっても便利ですよね。

しかしながらAtlasがspriteを明示的に表示してくれないがゆえに、そのPrefab用でないspriteを使ってしまうというミスも起きがちです。

例えば
CharaStatus.prefabには
・common
・charaStatus
を使う想定でいたのに、charaStatusAtlasに入っていないSpriteを一つ混入しただけで、1024×1024だかの余計なメモリを読み込むことになってしまいます。
これを人力でチェックするのは結構大変。。。ということでエディタスクリプトで対応しました。

Editorスクリプトでの対応

CheckPackingTag.cs
    [MenuItem("Hoge/CheckPackingTag")]
    private static void CheckPackingTag()
    {
        Dictionary<string, int> tagDic = new Dictionary<string, int> ();
        tagDic.Add ("notPacked", 0);

        Selection.objects = Selection.activeGameObject.GetComponentsInChildren<Image>();

        foreach (Image img in Selection.objects) {
            if (img.sprite != null){
                //Debug.Log(img.sprite.name);
                string path = AssetDatabase.GetAssetPath(img.sprite.texture);
                TextureImporter imp = AssetImporter.GetAtPath(path) as TextureImporter;
                if (imp != null){
                    if (string.IsNullOrEmpty(imp.spritePackingTag)){
                        tagDic["notPacked"] = tagDic["notPacked"] + 1;
                    }else{
                        if (tagDic.ContainsKey(imp.spritePackingTag) == false){
                            tagDic.Add(imp.spritePackingTag, 0);
                        }
                        tagDic[imp.spritePackingTag] = tagDic[imp.spritePackingTag] + 1;
                    }
                }
            }
        }
         Debug.Log("Summary");
        foreach (var key in tagDic.Keys) {
            Debug.Log(key + ":" + tagDic[key].ToString());
        }
    }

やっていることは
1. 触っているPrefabの中のImageを全て舐める
2. SpriteからTextureImporterを生成
3. packingTagを集計
4. 仲間外れを探せ!

こんな感じで、幸せになれるかと思います。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3