Beispiel #1
0
        /// <summary>
        /// ResourceDep系统专用的打包AssetBundle函数
        /// </summary>
        /// <param name="asset"></param>
        /// <param name="path"></param>
        /// <param name="depFileRelativeBuildPath">依赖文件列表,相对的AssetBundle打包路径</param>
        /// <param name="buildTarget"></param>
        /// <param name="quality"></param>
        /// <returns></returns>
        private static BuildBundleResult BuildAssetBundle(Object asset, string path,
                                                          IList <string> depFileRelativeBuildPath, BuildTarget buildTarget, KResourceQuality quality)
        {
            //是否是Level / Scene
            var isScene = asset.ToString().Contains("SceneAsset");

            uint crc  = 0;
            var  time = DateTime.Now;
            // 最终完整路径
            var buildToFullPath = KBuildTools.MakeSureExportPath(path, buildTarget, quality) +
                                  AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
            var buildToRelativePath = AbsPath2RelativePath(buildToFullPath);//buildToFullPath.Replace(workdirPath, "").Substring(1); // 转换成相对路径,避免路径过程无法打包的问题

            var assetPath = AssetDatabase.GetAssetPath(asset);

            // 版本标记
            var unityAssetType = GetUnityAssetType(assetPath);

            if (unityAssetType == UnityAssetType.Builtin || unityAssetType == UnityAssetType.Memory)
            {
                var buildAssetPath = GetBuildAssetPath(asset);
                KAssetVersionControl.TryMarkRecord(buildAssetPath);
                BuildedPool.Add(buildAssetPath);
            }
            else
            {
                KAssetVersionControl.TryMarkBuildVersion(assetPath);
                BuildedPool.Add(assetPath);
            }

            bool result = false;

            if (isScene)
            {
                var resultStr = BuildPipeline.BuildStreamedSceneAssetBundle(new string[] { assetPath }, buildToRelativePath,
                                                                            buildTarget, out crc);
                result = String.IsNullOrEmpty(resultStr);
                if (!String.IsNullOrEmpty(resultStr))
                {
                    Debug.LogError(resultStr);
                }
            }
            else
            {
                result = BuildPipeline.BuildAssetBundle(asset, null, buildToRelativePath,
                                                        out crc,
                                                        BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle |
                                                        BuildAssetBundleOptions.CompleteAssets,
                                                        buildTarget);
            }

            // 创建依赖记录文件
            string fullManifestPath = null;

            //if (depFileRelativeBuildPath != null && depFileRelativeBuildPath.Any())
            {
                //var manifestFileContent = string.Join("\n", depFileRelativeBuildPath.KToArray());
                if (depFileRelativeBuildPath == null)
                {
                    depFileRelativeBuildPath = new List <string>();
                }

                var manifestPath = path + ".manifest";
                fullManifestPath = KBuildTools.MakeSureExportPath(manifestPath, buildTarget, quality) +
                                   AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
                var relativeManifestPath = AbsPath2RelativePath(fullManifestPath); // 转成相对路径

                var utf8NoBom = new UTF8Encoding(false);
                //try
                //{
                File.WriteAllLines(relativeManifestPath, depFileRelativeBuildPath.KToArray(), utf8NoBom);
                //}
                //catch (Exception e)
                //{
                //    // 会出现DirectoryNotFound,但是目录明明就存在! 先Catch
                //    Logger.LogError("Exception: {0}", e.Message);
                //    var dirPath = Path.GetDirectoryName(fullManifestPath);
                //    if (Directory.Exists(dirPath))
                //        Logger.LogError("Directory Exists: {0}", dirPath);
                //    else
                //    {
                //        Logger.LogError("Directory not exists: {0}", dirPath);
                //    }
                //}
            }

            if (result)
            {
                var abInfo = new FileInfo(buildToFullPath);
                Logger.Log("Build AssetBundle: {0} / CRC: {1} / Time: {2:F4}s / Size: {3:F3}KB / FullPath: {4} / RelPath: {5}", path, crc, (DateTime.Now - time).TotalSeconds,
                           abInfo.Length / 1024d, buildToFullPath, buildToRelativePath);
            }
            else
            {
                Logger.LogError("生成文件失败: {0}, crc: {1} 耗时: {2:F5}, 完整路径: {3}", path, crc,
                                (DateTime.Now - time).TotalSeconds, buildToFullPath);
            }
            return(new BuildBundleResult
            {
                Crc = crc,
                FullPath = buildToFullPath,
                RelativePath = path,
                IsSuccess = result,
                ManifestFullPath = fullManifestPath,
            });
        }