Unity检测AssetBundle是否循环依赖

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

cs 复制代码
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("---------结束--------");
    }
}

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

相关推荐
90后小陈老师10 小时前
Unity教学 项目2 2D闯关游戏
游戏·unity·游戏引擎
噗噗夹的TA之旅10 小时前
Unity Shader 学习20:URP LitForwardPass PBR 解析
学习·unity·游戏引擎·图形渲染·技术美术
nnsix10 小时前
Unity ReferenceFinder插件 多选资源查找bug解决
unity·游戏引擎·bug
gzroy12 小时前
Unity Shader Graph实现全息瞄准器
unity·游戏引擎
90后小陈老师15 小时前
Unity教学 基础介绍
unity·游戏引擎
90后小陈老师15 小时前
Unity教学 项目3 3D坦克大战
3d·unity·游戏引擎
秦奈17 小时前
Unity复习学习随笔(五):Unity基础
学习·unity·游戏引擎
nnsix18 小时前
Unity ReferenceFinder插件 窗口中选择资源时 同步选择Assets下的资源
java·unity·游戏引擎
麷飞花19 小时前
unity3d scene窗口选中物体, 在 hierarchy高光显示
unity·editor·unity3d·u3d·hierarchy
ۓ明哲ڪ20 小时前
Unity功能——关闭脚本自动编译(Unity2021.3)
unity·游戏引擎