예제 #1
0
    /// <summary>
    /// 根据versionName获取需要打包的所有资源并返回AssetBundleBuild数组
    /// </summary>
    /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param>
    /// <returns>返回一个数组,可以直接用于BuildPipeline.BuildAssetBundles</returns>
    private static AssetBundleBuild[] GetBuildMapByVersion(string versionName)
    {
        List <AssetBundleBuild> list = new List <AssetBundleBuild>();

        if (!Directory.Exists(ABSH.NeedBuildABPath))
        {
            return(null);
        }
        var NeedBuildABFileList = GetAllFilesWithoutMeta(ABSH.NeedBuildABPath);
        var dicDependencies     = new Dictionary <string, string>();

        //遍历这些文件去找到所有的依赖项
        foreach (var file in NeedBuildABFileList)
        {
            //添加正向依赖,这个不可能会重复,所以不用判断ContainsKey
            ABDependenciesPositive.Add(file.Key, ABDependenciesPositive.GetDRAC(file.Key));
            //var relativePath = Path.Combine();
            var dps = AssetDatabase.GetDependencies(file.Key);
            //                                                                         这里文件名进行了转换,windows下都变成\\,OSX下都是/
            //也转换了一下小写
            foreach (var dp in dps.Where(d => !d.EndsWith(".cs")).Select(d => AssetBundleSettingHelper.PathToPlatformFormat(d.ToLower())))           //脚本文件排除
            {
                {
                    //增加反向依赖,这个会统计数量
                    if (!file.Key.Equals(dp))
                    {
                        if (!ABDependenciesReverse.ContainsKey(dp))
                        {
                            ABDependenciesReverse.Add(dp, ABDependenciesReverse.GetDRAC(dp));
                        }
                        ABDependenciesReverse[dp].AddRef(file.Key);

                        //正向依赖
                        ABDependenciesPositive[file.Key].AddRef(dp);
                    }
                }
                if (dicDependencies.ContainsKey(dp))
                {
                    continue;
                }
                else
                {
                    dicDependencies.Add(dp, dp);
                }
            }
        }
        //这里根据依赖关系,把只被一个单独AB依赖的资源不设置单独打包
        foreach (var abd in ABDependenciesReverse.GetDic().Where(dr => dr.Value.Count == 1))
        {
            if (dicDependencies.ContainsKey(abd.Key))
            {
                dicDependencies.Remove(abd.Key);
            }
        }
        //这里已经获得了所有的资源名称,可以直接生成AssetBundleBuild了
        foreach (var file in dicDependencies)
        {
            //目前先把所有资源单独打包,不做任何合并处理
            var tmp = new AssetBundleBuild();
            {
                tmp.assetBundleName = ABSH.ResourceNameToBundleName(file.Key);
                string[] assets = new string[1] {
                    file.Key
                };
                tmp.assetNames = assets;
            }
            list.Add(tmp);
        }

        return(list.ToArray());
    }