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

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

相关推荐
仙魁XAN4 小时前
Unity 设计模式 之 创造型模式-【工厂方法模式】【抽象工厂模式】
unity·设计模式·工厂方法模式·抽象工厂模式
我要吐泡泡了哦6 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
躺下睡觉~14 小时前
Unity-Transform类-父子关系
java·unity·游戏引擎
躺下睡觉~14 小时前
Unity-Transform类-缩放和看向
unity·游戏引擎
君莫愁。16 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
咩咩觉主17 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
蓝裕安20 小时前
伪工厂模式制造敌人
开发语言·unity·游戏引擎
谢泽浩1 天前
Unity 给模型贴上照片
unity·游戏引擎
z2014z1 天前
Unity Resource System 优化笔记
unity·游戏引擎
王维志1 天前
Unity 高亮插件HighlightPlus介绍
unity·游戏引擎