1. 程式人生 > 實用技巧 >NGUI查詢未使用圖集、未使用精靈及名字重複的精靈

NGUI查詢未使用圖集、未使用精靈及名字重複的精靈

1.遍歷atlasPath目錄,找到需要處理的所有圖集。
2.查詢ui預製體目錄,查詢目錄下的預製體的所有UISprite元件,獲取該元件的圖集、spriteName屬性,設定圖集的使用次數情況。
3.輸出unuseAtlas、unuseSprites、duplicateSprites三個陣列,並寫入到Assets/UnuseAtlas.json檔案中。
/// <summary>
    /// 查詢名字重複、未使用的圖集和精靈
    /// 只查詢預製體直接引用,不考慮配置表引用
    /// </summary>
    [MenuItem("Tools/查詢未使用的圖集和精靈")]
    public static void FindUnusualAltas() 
    {
        Dictionary<INGUIAtlas, Dictionary<string, int>> atlasInfo = new Dictionary<INGUIAtlas, Dictionary<string, int>>();
        string[] atlasPath = new string[] { "Assets/Design/Interface/Atlas"};    // TODO 指定專案裡圖集的父節點 需修改
        string[] uiPrefabsPath = new string[] { "Assets/Design/Interface/Prefab"};    // TODO 指定專案裡ui預製體的父節點 需修改
        string[] rejectAtlas = new string[] {
            "bp_big_icon", "data_f", "int_achievements_", "int_atlas_clan_ico_2",
            "int_avatars", "int_boost", "int_frame","product_icon_atlas", "RankSmallIcon", "RankSmallIcon"
        };    // TODO 過濾圖集,例如道具等在配置檔案配置的圖集 需修改
        
        string[] atlasGuids = AssetDatabase.FindAssets("t:NGUIAtlas", atlasPath);
        Dictionary<string, List<string>> spriteNameDic = new Dictionary<string, List<string>>();
        
        if (atlasGuids!= null && atlasGuids.Length > 0)
        {
            foreach (string  guid in atlasGuids)
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);
                NGUIAtlas obj = AssetDatabase.LoadAssetAtPath(path, typeof(NGUIAtlas)) as NGUIAtlas;

                if (obj) {
                    string atlaName = obj.name;
                    bool isReject = false;

                    foreach (var reject in rejectAtlas) {
                        if (atlaName.Contains(reject)) {
                            isReject = true;
                            break;
                        }
                    }
                    
                    if (isReject) continue;

                    List<UISpriteData> spriteList = obj.spriteList;

                    if (spriteList != null && spriteList.Count > 0) {
                        Dictionary<string, int> spriteDic = new Dictionary<string, int>();
                        for (int i = 0; i < spriteList.Count - 1; i++) {
                            string spriteName = spriteList[i].name;
                            spriteDic.Add(spriteName, 0);

                            List<string> nameList;
                            if (spriteNameDic.TryGetValue(spriteName, out nameList)) {
                                nameList.Add(obj.name);
                            } else {
                                nameList = new List<string>{obj.name};
                                spriteNameDic.Add(spriteName, nameList);
                            }
                        }
                    
                        atlasInfo.Add(obj, spriteDic);
                    }
                }
            }
        }
        
        string[] guids = AssetDatabase.FindAssets("t:Prefab", uiPrefabsPath);
        
        if (guids!= null && guids.Length > 0)
        {
            EditorUtility.DisplayProgressBar("Find...", "Start handle", 0);
            int progress = 0;
            foreach (string  guid in guids)
            {
                progress++;
                string path = AssetDatabase.GUIDToAssetPath(guid);
                GameObject obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
                
                UISprite[] sprites = obj.GetComponentsInChildren<UISprite>(true);
                foreach (UISprite item in sprites) {
                    Dictionary<string, int> spriteDic;
                    if (item.atlas != null && atlasInfo.TryGetValue(item.atlas, out spriteDic)) {
                        string spriteName = item.spriteName;
                        int useTimes = 0;
                        
                        if (!string.IsNullOrEmpty(spriteName) && spriteDic.TryGetValue(spriteName, out useTimes)) 
                        {
                            spriteDic[spriteName] = useTimes + 1;
                        }
                    }
                }
                
                EditorUtility.ClearProgressBar();
            }
        }
        
        List<string> unuseAtlas = new List<string>();//沒有用到的圖集
        List<string> unuseSprites = new List<string>();//圖集內沒有用到的精靈
        List<string> duplicateSprites = new List<string>();//重複的圖片名字

        foreach (var atlaInfo in atlasInfo) {
            int useTimes = 0;
            string atlaName = (atlaInfo.Key as NGUIAtlas).name;

            foreach (var spriteInfo in atlaInfo.Value) 
            {
                if (spriteInfo.Value == 0) {
                    unuseSprites.Add(string.Format("未用到精靈 圖集:{0}, 精靈:{1}", atlaName, spriteInfo.Key));
                }

                useTimes += spriteInfo.Value;
            }

            if (useTimes == 0) {
                unuseAtlas.Add(string.Format("未用到圖集:{0}", atlaName));
            }
        }

        foreach (var nameList in spriteNameDic) {
            string duplacateStr = "";
            if (nameList.Value != null && nameList.Value.Count > 2) 
            {
                for (int i = 0; i < nameList.Value.Count - 1; i++) {
                    duplacateStr += nameList.Value[i] + ", ";
                }
                
                duplicateSprites.Add(string.Format("重複的精靈名字, 精靈名:{0},所在圖集:{1}", nameList.Key, duplacateStr));
            }
        }

        string line = "\n";
        var sb = new StringBuilder();
        sb.Append("//圖集檢索\n");
        sb.Append("//未使用圖集列表\n");
        sb.Append("unuseAtlas={\n");
        unuseAtlas.ForEach(item => sb.Append(String.Format("    {0},\n", item)));
        sb.Append("}\n");
        
        sb.Append("//未使用精靈列表\n");
        sb.Append("unuseSprites={\n");
        unuseSprites.ForEach(item => sb.Append(String.Format("    {0},\n", item)));
        sb.Append("}\n");
        
        sb.Append("//命名重複精靈\n");
        sb.Append("duplicateSprites={\n");
        duplicateSprites.ForEach(item => sb.Append(String.Format("    {0}\n", item)));
        sb.Append("}\n");
        
        System.IO.File.WriteAllText(Path.Combine(Application.dataPath, "UnuseAtlas.json"), sb.ToString());
        
        GGDebug.Debug(sb.ToString());
        AssetDatabase.SaveAssets();
    }

 最終輸出格式:

//圖集檢索
//未使用圖集列表
unuseAtlas={
    未用到圖集:int_atlas_back_result,
    未用到圖集:int_atlas_back_strategic,
    未用到圖集:int_atlas_clan_ico_1,
    未用到圖集:int_atlas_icons,
    未用到圖集:int_bind_phone,
    未用到圖集:int_boxes,
    未用到圖集:int_direct_spawn,
    未用到圖集:int_emblem_conf,
    未用到圖集:int_emblem_res,
    未用到圖集:int_header_images,
    未用到圖集:int_ico_units_f2,
    未用到圖集:int_medals,
    未用到圖集:int_packs,
    未用到圖集:int_panel_darken,
    未用到圖集:int_person_f1_doc_minister,
    未用到圖集:int_person_f1_general_colonnel,
    未用到圖集:int_person_f1_secretary,
    未用到圖集:int_person_f2_commander_tech,
    未用到圖集:int_person_f2_general_saboteur,
    未用到圖集:int_person_f2_infantry_moloh,
    未用到圖集:int_ranks,
    未用到圖集:int_ranks_atlas,
    未用到圖集:panel_home_atlas,
    未用到圖集:ready_battle_ Atlas,
}
//未使用精靈列表
unuseSprites={
    未用到精靈 圖集:Backpack_Atlas, 精靈:bg_03,
    未用到精靈 圖集:Backpack_Atlas, 精靈:bg_tv,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_01,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_02_off,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_03_off,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_04,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_04_off,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_3,
    未用到精靈 圖集:Backpack_Atlas, 精靈:ic_play ,
    未用到精靈 圖集:mall, 精靈:btn_shop,
    未用到精靈 圖集:mall, 精靈:common_txt,
    未用到精靈 圖集:mall, 精靈:diamonds_ic,
    未用到精靈 圖集:mall, 精靈:level_number_0,
    未用到精靈 圖集:mall, 精靈:line_bg,
    未用到精靈 圖集:mall, 精靈:lucky_bg09,
    未用到精靈 圖集:mall, 精靈:lucky_bg12,
    未用到精靈 圖集:mall, 精靈:lucky_bg21,
    未用到精靈 圖集:mall, 精靈:mall_0,
    未用到精靈 圖集:mall, 精靈:shop_bg01,
    未用到精靈 圖集:mall, 精靈:shop_label02,
    未用到精靈 圖集:rank_list, 精靈:ic_01,
    未用到精靈 圖集:rank_list, 精靈:me_bg,
    未用到精靈 圖集:rank_list, 精靈:size_02,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_colonel_2,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_colonel_3,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_general_1,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_general_2,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_general_3,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_greatMarshal,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_greatMarshal_1,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_greatMarshal_2,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_honor,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_honor_1,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_lieutenant_2,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_lieutenant_3,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_marshal_1,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_marshal_2,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_marshal_3,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_marshal_5,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_sergeant_2,
    未用到精靈 圖集:RankBigIconAtlas, 精靈:ic_sergeant_3,
    未用到精靈 圖集:ranking_02, 精靈:btn_bank,
    未用到精靈 圖集:ranking_02, 精靈:btn_bg02,
    未用到精靈 圖集:ranking_02, 精靈:btn_x,
    未用到精靈 圖集:ranking_02, 精靈:ic_bg,
    未用到精靈 圖集:ranking_02, 精靈:ic_bg2,
    未用到精靈 圖集:ranking_02, 精靈:xbg_01,
}
//命名重複精靈
duplicateSprites={
    重複的精靈名字, 精靈名:bg_01,所在圖集:Backpack_Atlas, battle_result, battle_ui, int_common_ui, int_daily_sign, int_mail, int_normal_battle, int_personal_info, int_record, panel_talent_atlas, ,
    重複的精靈名字, 精靈名:bg_02,所在圖集:Backpack_Atlas, battle_result, battle_ui, int_activity, int_daily_sign, int_mail, int_normal_battle, int_record, login_atlas, panel_talent_atlas, ,
    重複的精靈名字, 精靈名:bg_03,所在圖集:Backpack_Atlas, battle_ui, int_activity, int_daily_sign, int_mail, int_normal_battle, int_record, login_atlas, ,
    重複的精靈名字, 精靈名:ic_01,所在圖集:Backpack_Atlas, battle_ui, int_clan, int_record, login_atlas, panel_talent_atlas, rank_list, ,
    重複的精靈名字, 精靈名:ic_02,所在圖集:Backpack_Atlas, battle_ui, int_record, login_atlas, panel_talent_atlas, rank_list, ,
    重複的精靈名字, 精靈名:ic_04,所在圖集:Backpack_Atlas, battle_ui, int_common_ui, int_record, login_atlas, panel_talent_atlas, ,
    重複的精靈名字, 精靈名:1,所在圖集:battle_result, battle_ui, campaign_task, friend, int_common_ui, loading, product_big_icon, ,
    重複的精靈名字, 精靈名:2,所在圖集:battle_result, battle_ui, campaign_task, friend, int_common_ui, loading, product_big_icon, ,
    重複的精靈名字, 精靈名:3,所在圖集:battle_result, battle_ui, campaign_task, int_common_ui, loading, product_big_icon, ,
    重複的精靈名字, 精靈名:4,所在圖集:battle_result, campaign_task, int_common_ui, ,
    重複的精靈名字, 精靈名:5,所在圖集:battle_result, campaign_task, ,
}