Unity简单资源加载框架(三):打包AB包二(记录依赖文件)

/****************************************************
    文件:ABEditor.cs
	作者:赵深圳
    邮箱: 1329346609@qq.com
    日期:2022/5/7 15:54:51
	功能:AB包编辑器
*****************************************************/

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

/// <summary>
/// 生成AB包编辑器工具
/// </summary>
public class ABEditor  
{
    /// <summary>
    /// 需要打包的资源所在目标
    /// </summary>
    public static string rootPath = Application.dataPath + "/1SimpleABManager/Download";

    /// <summary>
    /// AB包输出路径
    /// </summary>
    public static string outABPath=Application.streamingAssetsPath;

    /// <summary>
    /// 记录每个资源所有在的AB包文件
    /// key为路径
    /// value为AB包名字
    /// </summary>
    public static Dictionary<string, string> m_Asset2BundleDic = new Dictionary<string, string>();

    /// <summary>
    /// 所有需要打包的资源对应的AssetBundleBuild
    /// 一个文件对应一个AssetBundleBuild
    /// </summary>
    public static List<AssetBundleBuild> m_AssetBundleBuildLst = new List<AssetBundleBuild>();

    /// <summary>
    /// 记录每个Asset资源所依赖的AB包文件
    /// key:Asset名字
    /// Value:每个资源所依赖的AB包列表
    /// </summary>
    public static Dictionary<string, List<string>> m_Asset2DepenendciesDic = new Dictionary<string, List<string>>();

    /// <summary>
    /// 打包AB包
    /// </summary>
    [MenuItem("ZSTools/BuildABTools")]
    public static void BuildAssetBundle() 
    {
        Log.ZSZLog(LogCategory.Normal,"开始打AB包!!!");

        if (Directory.Exists(outABPath))
        {
            Directory.Delete(outABPath,true);
        }

        //打包所有的文件夹

        DirectoryInfo rootDir = new DirectoryInfo(rootPath);
        //获取资源路径下所有的文件夹
        //这里获取的文件夹,不包括自身,所有在资源所在文件(Application.dataPath + "/1SimpleABManager/Download")下不要放打包资源,会打包不到
        //放在资源所在文件所在的下一级文件,如果需要放在资源文件夹中,请把自身加入到数组中
        DirectoryInfo[] dirs = rootDir.GetDirectories();     
        foreach (DirectoryInfo moduleDir in dirs)
        {
            string moduleName = moduleDir.Name;
            m_AssetBundleBuildLst.Clear();
            m_Asset2BundleDic.Clear();
            ScanChildDireations(moduleDir);
            AssetDatabase.Refresh();
            string moduleOutPath = outABPath + "/" + moduleName;
            if (Directory.Exists(moduleOutPath))
            {
                Directory.Delete(moduleOutPath, true);
            }
            Directory.CreateDirectory(moduleOutPath);
            //打包
            BuildPipeline.BuildAssetBundles(moduleOutPath,m_AssetBundleBuildLst.ToArray(),BuildAssetBundleOptions.None,EditorUserBuildSettings.activeBuildTarget);
            CalAssetDependencies();
            AssetDatabase.Refresh();
        }

        Log.ZSZLog(LogCategory.Normal,"打包AB包结束!!!");
    }

    /// <summary>
    /// 根据指定的文件夹
    /// 将这个文件夹下所有的一级子文件夹打成一个AB包
    /// 递归这个文件夹下所有的子文件夹
    /// </summary>
    /// <param name="directoryInfo"></param>
    private static void ScanChildDireations(DirectoryInfo directoryInfo)
    {
        if (directoryInfo.Name.EndsWith("CSProject")|| directoryInfo.Name.EndsWith(".cs"))
        {
            return;
        }

        ScanCurrDirectory(directoryInfo);
        
        //递归当前路径下的所有文件夹
        //使所有的一级文件夹为一个模块
        DirectoryInfo[] dirs = directoryInfo.GetDirectories();
        foreach (var item in dirs)
        {
            ScanChildDireations(item);
        }
    }

    /// <summary>
    /// 遍历当前路径下的文件,并打成一个AB包
    /// </summary>
    /// <param name="directoryInfo"></param>
    private static void ScanCurrDirectory(DirectoryInfo directoryInfo) 
    {
        List<string> assetNameLst = new List<string>();

        //获取指定文件夹中所有的文件
        FileInfo[] fileInfoArr = directoryInfo.GetFiles();

        foreach (FileInfo fileInfo in fileInfoArr)
        {
            if (fileInfo.FullName.EndsWith(".meta"))
            {
                continue;
            }
            //assetName的格式类似 "Assets/GAssets/Launch/Sphere.prefab"
            string assetName = fileInfo.FullName.Substring(Application.dataPath.Length-"Assets".Length).Replace('\\','/');
            assetNameLst.Add(assetName);
        }

        if (assetNameLst.Count > 0)
        {
            string assetBundleName = directoryInfo.FullName.Substring(Application.dataPath.Length + 1).Replace('\\','_').ToLower();
            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = assetBundleName;
            build.assetNames = new string[assetNameLst.Count];
            for (int i = 0; i < assetNameLst.Count; i++)
            {
                build.assetNames[i] = assetNameLst[i];
                m_Asset2BundleDic.Add(assetNameLst[i],assetBundleName);
            }
            m_AssetBundleBuildLst.Add(build);
        }
    }

    /// <summary>
    /// 计算每个资源依赖的AB包列表
    /// </summary>
    private static void CalAssetDependencies()
    {
        foreach (string asset in m_Asset2BundleDic.Keys)
        {
            //当前资源所在的AB包
            string assetBundle = m_Asset2BundleDic[asset];
            //获取当前资源所有的依赖
            string[] dependencies = AssetDatabase.GetDependencies(asset);
            List<string> assetLst = new List<string>();
            //获取当前所有依赖的资源的名字
            if (dependencies != null && dependencies.Length > 0)
            {
                foreach (string oneAsset in dependencies)
                {
                    if (oneAsset==asset||oneAsset.EndsWith(".cs"))
                    {
                        continue;
                    }
                    assetLst.Add(oneAsset);
                }
            }
            if (assetLst.Count > 0)
            {
                List<string> abList = new List<string>();
                //获取当前所有的依赖资源所在的ab包
                foreach (string oneAsset in assetLst)
                {
                    bool result= m_Asset2BundleDic.TryGetValue(oneAsset,out string bundle);
                    if (result==true)
                    {
                        if (bundle!= assetBundle)
                        {
                            abList.Add(bundle);
                        }
                    }
                }
                //存入依赖字典中
                m_Asset2DepenendciesDic.Add(asset,abList);
            }

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值