Unity检测AssetBundle是否循环依赖

原理:bundle的依赖关系构建一个二维的矩阵图,如果对角线相互依赖(用1标记)则表示循环依赖。

using PlasticGui;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class DependProfiler
{
    [MenuItem("Tools/CircleBundleDepend")]
    static void Depend() {
        string path = Application.streamingAssetsPath +"/StreamingAssets";
        AssetBundleManifest manifest =  BundleGroupEditor.ParseManifest(path);
        var bundles = manifest.GetAllAssetBundles();
        //bundle的依赖矩阵,如果对角值为1表示循环依赖
        string[,] bundleMatrix = new string[bundles.Length+1, bundles.Length+1];
        //添加表头
        for (int i = 1; i < bundleMatrix.GetLength(0); i++)
        {
            var bundle = bundles[i-1];
            bundleMatrix[i, 0] = bundle;
            for(int j=1; j< bundleMatrix.GetLength(1); j++)
            {
                bundleMatrix[i, j] = "0";
            }
            bundleMatrix[0, i] = bundle;
        }
        //判定依赖关系
        for (int i = 1; i < bundleMatrix.GetLength(0); i++)
        {
            var bundle = bundleMatrix[i, 0];
            var depends = manifest.GetAllDependencies(bundle);
            if (depends.Length > 0)
            {
                for (int j = 0; j < depends.Length; j++) {
                    var depend = depends[j];
                    for(int k=1; k< bundleMatrix.GetLength(1); k++)
                    {
                        if (bundleMatrix[0,k].Equals(depend))
                        {
                            bundleMatrix[i, k] = "1";
                            break;
                        }
                    }
                }
            }
        }

        for(int i = 0;i < bundleMatrix.GetLength(0); i++)
        {
            for(int j = 0;j < bundleMatrix.GetLength(1); j++) {
                if (bundleMatrix[i,j]=="1" && bundleMatrix[j, i] == "1")
                {
                    Debug.LogError(string.Format("{0} - {1}", bundleMatrix[i, 0], bundleMatrix[0, j]));
                }
            }
        }
        //判定是否循环依赖
        Debug.Log("---------结束--------");
    }
}

 下载资源以后编辑器运行即可看到如下的依赖关系

相关推荐

  1. Unity AssetBundle学习笔记

    2023-12-13 06:40:03       33 阅读
  2. 如何拆解Unity 2022.3版本的AssetBundle

    2023-12-13 06:40:03       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-13 06:40:03       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-13 06:40:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-13 06:40:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-13 06:40:03       18 阅读

热门阅读

  1. 【告警自动化处置脚本】

    2023-12-13 06:40:03       37 阅读
  2. 【环境系列】Linux虚拟机(Centos、Ubuntu)、云服务器

    2023-12-13 06:40:03       38 阅读
  3. webpack的使用

    2023-12-13 06:40:03       37 阅读
  4. 微信小程序---生命周期

    2023-12-13 06:40:03       42 阅读
  5. 微信小程序下载 base64 视频文件到本地相册

    2023-12-13 06:40:03       40 阅读
  6. kafka学习笔记--节点的服役与退役

    2023-12-13 06:40:03       46 阅读
  7. 小程序猎鹰策略组件

    2023-12-13 06:40:03       37 阅读
  8. 《C++新经典设计模式》之第2章 模板方法模式

    2023-12-13 06:40:03       33 阅读
  9. Spark SQL 的partitionBy() 动态分区

    2023-12-13 06:40:03       34 阅读