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("---------结束--------");
    }
}

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

相关推荐
郝学胜-神的一滴2 小时前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建
淡海水3 小时前
02-01-原理篇-Mark-Sweep与变种算法
算法·unity·c#·游戏引擎·.net·gc
babywang04 小时前
Unity笔记--塔防游戏中的ECS架构:从原理到实战的全面解析
unity·ecs架构
babywang01 天前
Unity笔记--AssetBundle详解
unity·游戏引擎
魔士于安1 天前
经典Game
游戏·游戏引擎·贴图·技术美术·模型
淡海水1 天前
01-04-认知篇-Unity内存全景
unity·c#·游戏引擎·.net·gc
598866753@qq.com2 天前
Codex与Unity
unity·游戏引擎
淡海水2 天前
01-03-认知篇-C#内存模型深度解析
unity·c#·游戏引擎·.net·gc
警醒与鞭策2 天前
Direct3D 12 中的内存管理
3d·游戏引擎·direct3d
淡海水2 天前
16-02-C#常用数据结构源码-附录B-源码索引速查
unity·c#·游戏引擎·il2cpp