/// <summary> /// Extra Flag -> ex: Android/ AndroidSD/ AndroidHD/ /// </summary> /// <param name="platfrom"></param> /// <param name="quality"></param> /// <returns></returns> public static string GetExportPath(BuildTarget platfrom, KResourceQuality quality = KResourceQuality.Sd) { string basePath = Path.GetFullPath(KEngine.AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleBuildRelPath)); if (File.Exists(basePath)) { BuildTools.ShowDialog("路径配置错误: " + basePath); throw new System.Exception("路径配置错误"); } if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } string path = null; var platformName = KResourceModule.BuildPlatformName; if (quality != KResourceQuality.Sd) // SD no need add { platformName += quality.ToString().ToUpper(); } path = basePath + "/" + platformName + "/"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } return(path); }
/// <summary> /// 获取完整的打包路径,并确保目录存在 /// </summary> /// <param name="path"></param> /// <param name="buildTarget"></param> /// <returns></returns> public static string MakeSureExportPath(string path, BuildTarget buildTarget, KResourceQuality quality) { path = KBuildTools.GetExportPath(buildTarget, quality) + path; string exportDirectory = path.Substring(0, path.LastIndexOf('/')); if (!System.IO.Directory.Exists(exportDirectory)) { System.IO.Directory.CreateDirectory(exportDirectory); } path = path.Replace("/", @"\"); return(path); }
public static uint BuildScriptableObject <T> (T scriptObject, string path, BuildTarget buildTarget, KResourceQuality quality) where T : ScriptableObject { const string tempAssetPath = "Assets/~Temp.asset"; AssetDatabase.CreateAsset(scriptObject, tempAssetPath); T tempObj = (T)AssetDatabase.LoadAssetAtPath(tempAssetPath, typeof(T)); if (tempObj == null) { throw new System.Exception(); } uint crc = BuildTools.BuildAssetBundle(tempObj, path, buildTarget, quality); AssetDatabase.DeleteAsset(tempAssetPath); return(crc); }
/// <summary> /// Extra Flag -> ex: Android/ AndroidSD/ AndroidHD/ /// </summary> /// <param name="platfrom"></param> /// <param name="quality"></param> /// <returns></returns> public static string GetExportPath(BuildTarget platfrom, KResourceQuality quality = KResourceQuality.Sd) { string basePath = Path.GetFullPath(Application.dataPath + "/" + KEngine.AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleBuildRelPath) + "/"); if (File.Exists(basePath)) { KBuildTools.ShowDialog("路径配置错误: " + basePath); throw new System.Exception("路径配置错误"); } if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } string path = null; switch (platfrom) { case BuildTarget.Android: case BuildTarget.iPhone: case BuildTarget.StandaloneWindows: var platformName = KResourceModule.BuildPlatformName; if (quality != KResourceQuality.Sd) // SD no need add { platformName += quality.ToString().ToUpper(); } path = basePath + platformName + "/"; break; default: KBuildTools.ShowDialog("构建平台配置错误"); throw new System.Exception("构建平台配置错误"); } return(path); }
public static uint BuildAssetBundle(Object asset, string path, BuildTarget buildTarget, KResourceQuality quality) { if (asset == null || string.IsNullOrEmpty(path)) { BuildError("BuildAssetBundle: {0}", path); return(0); } var assetNameWithoutDir = asset.name.Replace("/", "").Replace("\\", ""); // 防止多重目录... string tmpPrefabPath = string.Format("Assets/{0}.prefab", assetNameWithoutDir); PrefabType prefabType = PrefabUtility.GetPrefabType(asset); string relativePath = path; path = MakeSureExportPath(path, buildTarget, quality); var assetPath = AssetDatabase.GetAssetPath(asset); CheckAndLogDependencies(assetPath); uint crc = 0; if (asset is Texture2D) { if (!string.IsNullOrEmpty(assetPath)) // Assets内的纹理 // Texutre不复制拷贝一份 { _DoBuild(out crc, asset, null, path, relativePath, buildTarget); } else { // 内存的图片~临时创建Asset, 纯正的图片, 使用Sprite吧 var memoryTexture = asset as Texture2D; var memTexName = memoryTexture.name; var tmpTexPath = string.Format("Assets/Tex_{0}_{1}.png", memoryTexture.name, Path.GetRandomFileName()); Log.Warning("【BuildAssetBundle】Build一个非Asset 的Texture: {0}", memoryTexture.name); File.WriteAllBytes(tmpTexPath, memoryTexture.EncodeToPNG()); AssetDatabase.ImportAsset(tmpTexPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); var tmpTex = (Texture2D)AssetDatabase.LoadAssetAtPath(tmpTexPath, typeof(Texture2D)); asset = tmpTex; try { asset.name = memTexName; _DoBuild(out crc, asset, null, path, relativePath, buildTarget); } catch (Exception e) { Log.LogException(e); } File.Delete(tmpTexPath); if (File.Exists(tmpTexPath + ".meta")) { File.Delete(tmpTexPath + ".meta"); } } } else if ((prefabType == PrefabType.None && assetPath == string.Empty) || (prefabType == PrefabType.ModelPrefabInstance)) // 非prefab对象 { Object tmpInsObj = (GameObject)GameObject.Instantiate(asset); // 拷出来创建Prefab Object tmpPrefab = PrefabUtility.CreatePrefab(tmpPrefabPath, (GameObject)tmpInsObj, ReplacePrefabOptions.Default); CheckAndLogDependencies(tmpPrefabPath); asset = tmpPrefab; _DoBuild(out crc, asset, null, path, relativePath, buildTarget); GameObject.DestroyImmediate(tmpInsObj); AssetDatabase.DeleteAsset(tmpPrefabPath); } else if (prefabType == PrefabType.PrefabInstance) { var prefabParent = PrefabUtility.GetPrefabParent(asset); _DoBuild(out crc, prefabParent, null, path, relativePath, buildTarget); } else { //Log.Error("[Wrong asse Type] {0}", asset.GetType()); _DoBuild(out crc, asset, null, path, relativePath, buildTarget); } return(crc); }
/// <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 = BuildTools.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); AssetVersionControl.TryMarkRecord(buildAssetPath); BuildedPool.Add(buildAssetPath); } else { AssetVersionControl.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 = BuildTools.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 // Log.Error("Exception: {0}", e.Message); // var dirPath = Path.GetDirectoryName(fullManifestPath); // if (Directory.Exists(dirPath)) // Log.Error("Directory Exists: {0}", dirPath); // else // { // Log.Error("Directory not exists: {0}", dirPath); // } //} } if (result) { var abInfo = new FileInfo(buildToFullPath); Log.Info("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 { Log.Error("生成文件失败: {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, }); }