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

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

相关推荐
!chen2 小时前
Unity颜色曲线ColorCurves
unity·游戏引擎
B0URNE2 小时前
【Unity基础详解】(4)Unity核心类:MonoBehaviour
unity·游戏引擎
AA陈超7 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-29 属性信息委托
c++·游戏·ue5·游戏引擎·虚幻
AA陈超9 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-31 映射标签到属性
c++·游戏·ue5·游戏引擎·虚幻
gshh__9 小时前
SuperMap Hi-Fi 3D SDK for Unreal 使用蓝图接口加载多源数据
ue5·游戏引擎·supermap
小时候的阳光9 小时前
Cocos Creator 和 Unity 3D 编辑界面字体样式大小调整
unity·cocos2d·字体大小
ellis19709 小时前
Lua代码混淆-Prometheus方案教程
unity·lua
EQ-雪梨蛋花汤11 小时前
【MRTK3踩坑记录】Unity 2022 中 MRTK3 Input Simulator 无法使用 WASD 控制相机的完整排查记录
数码相机·unity·游戏引擎
星夜泊客1 天前
Unity 游戏开发中的防御性编程与空值处理实践
unity·设计模式·游戏引擎
mit6.8241 天前
[无人机sdk] Open Protocol | 协议包构造&验证
游戏引擎·无人机·cocos2d