Unity查找资源依赖关系

这个方法主要是发现资源乱用的情况,对应的逻辑可能要改一个才能用到自己的项目里面 

[MenuItem("Tools/Prefab/查找选中资源依赖关系", false, 0)]
    public static void FindDependencies()
    {
        foreach (var guid in Selection.assetGUIDs)
        {
            string lpath = AssetDatabase.GUIDToAssetPath(guid);
            string dirName = Path.GetDirectoryName(lpath);
            string folderName = dirName.Substring(dirName.LastIndexOf("\\") + 1);
            Debug.Log("----------------------------------------------------------------");
            GameObject go = Selection.activeObject as GameObject;
            if (go == null)
            {
                Debug.Log("选取对象出错");
                return;
            }
            string assetPath = AssetDatabase.GetAssetPath(go);
            Dictionary<string, List<ObjStruct>> dePathDic = new Dictionary<string, List<ObjStruct>> ();
            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.Log("注意,这不是预制体");
                return;
            }
            //获取预制体内所有Image组件
            Image[] imgArr = go.GetComponentsInChildren<Image>();
            if (imgArr == null || imgArr.Length == 0)
            {
                Debug.Log("预制体里没有Image组件");
            }
            else
            {
                foreach (var item in imgArr)
                {
                    string path = AssetDatabase.GetAssetPath(item.sprite.GetInstanceID());
                    if (!dePathDic.ContainsKey(path))
                    {
                        dePathDic.Add(path, new List<ObjStruct> ());
                    }
                    ObjStruct objStruct = new ObjStruct();
                    if (item.sprite != null)
                    {
                        objStruct.spriteName = item.sprite.name;
                        objStruct.objName = item.transform.name;
                        dePathDic[path].Add(objStruct);
                    }
                }
            }
            foreach (var dependenciePath in AssetDatabase.GetDependencies(lpath, false))
            {
                if (lpath != dependenciePath && !dependenciePath.Contains("cs"))
                {
                    if (!dependenciePath.Contains(folderName) && !dependenciePath.Contains("UICommonRes") && !dependenciePath.Contains("Font"))
                    {
                        Debug.LogError(string.Format("资源使用不当 path = {0} 依赖 > {1}", lpath, dependenciePath));
                        if (dePathDic.TryGetValue(dependenciePath,out List<ObjStruct> list))
                        {
                            for (int i = 0; i < list.Count; i++)
                            {
                                Debug.LogError(string.Format("错误详情:对象名:{0} , Sprite名:{1}", list[i].objName, list[i].spriteName));
                            }
                        }
                    }
                    else
                    {
                        Debug.Log(string.Format("lpath = {0} 依赖 > {1}", lpath, dependenciePath));
                    }
                }
            }

            
        }
        Debug.Log("查找依赖结束");
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值