static void SaveAssetBundleOutputFile(AssetBundleHeader header, string assetBundleFilePath, string outputFilePath) { string outputFileDir = Path.GetDirectoryName(outputFilePath); Directory.CreateDirectory(outputFileDir); var oldOutputFileInfo = new FileInfo(outputFilePath); if (oldOutputFileInfo.Exists) { oldOutputFileInfo.IsReadOnly = false; } using (FileStream fs = File.Open(outputFilePath, FileMode.Create, FileAccess.Write)) { header.Save(fs); byte[] bundleContent = File.ReadAllBytes(assetBundleFilePath); fs.Write(bundleContent, 0, bundleContent.Length); } }
public static void BuildToOneBundle(Object realass, string path, bool recursive) { if (CheckModify && recursive) { if (!CheckAssetModify(realass, path, true)) { return; } } int AssetCount = 0; List <AssetBundleHeader.DepInfo> ls = new List <AssetBundleHeader.DepInfo>(); string asspath = AssetDatabase.GetAssetPath(realass); if (recursive) { string[] deppaths = AssetDatabase.GetDependencies(new string[] { asspath }); Dictionary <string, bool> realdeps = new Dictionary <string, bool>(); foreach (string realdep in deppaths) { realdeps[realdep] = true; } Object[] depobjs = EditorUtility.CollectDependencies(new Object[] { realass }); BuildPipeline.PushAssetDependencies(); AssetCount++; for (int i = 0; i < depobjs.Length; i++) { Object dep = depobjs[i]; if (ReferenceEquals(dep, realass)) { continue; } string texpath = AssetDatabase.GetAssetPath(dep); if (!realdeps.ContainsKey(texpath)) { continue; } if (dep is Texture || dep is Shader || dep is MonoScript || dep is Font) { if (!texpath.ToLower().EndsWith(".asset") && !string.IsNullOrEmpty(texpath)) { BuildToOneBundle(dep, path, false); AssetBundleHeader.DepInfo info = new AssetBundleHeader.DepInfo(); info.name = ""; string realname = ConvertFileName(texpath) + ".u3dext"; info.path = realname.ToLower(); ls.Add(info); } } } BuildPipeline.PushAssetDependencies(); AssetCount++; } string assetpath = ConvertFileName(Path.GetDirectoryName(asspath)); string outpath; if (asspath != "") { outpath = path + "/" + assetpath; } else { outpath = assetpath; } Directory.CreateDirectory(outpath); string guid = AssetDatabase.AssetPathToGUID(asspath); string outfile = outpath + "/" + guid; BuildAssetBundleOptions option; option = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies; option |= BuildAssetBundleOptions.UncompressedAssetBundle; bool suc = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile, option, ExportAssetBundlesHelper.CurBuildTarget); // do not compress font bool ForceSep = realass is Font; bool NeedSep = false; if (suc && !ForceSep) { FileInfo fi = new FileInfo(outfile); if (realass is AudioClip) { if (fi.Length > min_audio_clip_bundel_size) { NeedSep = true; } } else if (fi.Length > min_compress_bundle_size) { option = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies; suc = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile, option, ExportAssetBundlesHelper.CurBuildTarget); Debug.LogWarning("Big bundle: " + outfile + " Origin Size: " + fi.Length); } } for (int n = 0; n < AssetCount; n++) { BuildPipeline.PopAssetDependencies(); } if (suc) { byte[] content = File.ReadAllBytes(outfile); string outfile2 = outpath + "/" + Path.GetFileName(asspath) + ".u3dext"; using (FileStream fs = File.Open(outfile2, FileMode.Create, FileAccess.Write)) { AssetBundleHeader bh = new AssetBundleHeader(); if (ForceSep || NeedSep) { bh.option |= AssetBundleHeader.BundleOption.SepFile; } if (realass is Shader || realass is MonoScript || realass is Font) { bh.option |= AssetBundleHeader.BundleOption.DonotRelease; } bh.deps = ls.ToArray(); bh.Save(fs); fs.Write(content, 0, content.Length); } File.Delete(outfile); } else { Debug.LogError("BuildAssetBundleExplicitAssetNames"); } }
static void BuildOneAssetBundle(string expcur, Dictionary <string, int> objmark, Dictionary <string, List <string> > allobjchild, string path, ref int depcount) { // 假设已经输出则跳过 if (objmark.ContainsKey(expcur)) { return; } List <string> subs = allobjchild[expcur]; if (subs != null) { foreach (string sub in subs) { // 假设已经输出则跳过 if (objmark.ContainsKey(sub)) { continue; } BuildOneAssetBundle(sub, objmark, allobjchild, path, ref depcount); } } objmark[expcur] = 1; string asspath = expcur; if (string.IsNullOrEmpty(asspath)) { return; } string guid = AssetDatabase.AssetPathToGUID(asspath); string assetpath = ConvertFileName(Path.GetDirectoryName(asspath)); string outpath; if (asspath != "") { outpath = path + "/" + assetpath; } else { outpath = assetpath; } Directory.CreateDirectory(outpath); //string outfile = Path.Combine(outpath, Path.GetFileNameWithoutExtension(asspath)); string outfile = outpath + "/" + guid; BuildPipeline.PushAssetDependencies(); depcount++; Object realass = AssetDatabase.LoadMainAssetAtPath(expcur); if (realass == null) { return; } Object[] depobjs = EditorUtility.CollectDependencies(new Object[] { realass }); Dictionary <string, int> deppathmap = new Dictionary <string, int>(); foreach (Object depobj in depobjs) { string deppath = AssetDatabase.GetAssetPath(depobj); if (string.IsNullOrEmpty(deppath)) { continue; } deppathmap[deppath] = 1; } List <string> realsubs = new List <string>(); if (subs != null) { foreach (string sub in subs) { if (deppathmap.ContainsKey(sub)) { string realname = ConvertFileName(sub) + ".u3dext"; realsubs.Add(realname.ToLower()); } } } BuildAssetBundleOptions option; option = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies; option |= BuildAssetBundleOptions.UncompressedAssetBundle; bool suc = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile, option, ExportAssetBundlesHelper.CurBuildTarget); Debug.Log("src file: " + asspath + " " + depcount); if (/*realass is MonoScript || */ !deppathmap.ContainsKey(expcur)) { File.Delete(outfile); } else if (suc) { // do not compress font bool ForceSep = realass is Font; bool NeedSep = false; if (!ForceSep) { FileInfo fi = new FileInfo(outfile); if (realass is AudioClip) { if (fi.Length > min_audio_clip_bundel_size) { NeedSep = true; } } else if (fi.Length > min_compress_bundle_size) { option = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies; suc = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile, option, ExportAssetBundlesHelper.CurBuildTarget); Debug.LogWarning("Big bundle: " + outfile + " Origin Size: " + fi.Length); } } byte[] content = File.ReadAllBytes(outfile); string outfile2 = outpath + "/" + Path.GetFileName(asspath) + ".u3dext"; _depmap[outfile2.ToLower()] = 1; var oldFileInfo = new FileInfo(outfile2); if (oldFileInfo.Exists) { oldFileInfo.IsReadOnly = false; } using (FileStream fs = File.Open(outfile2, FileMode.Create, FileAccess.Write)) { AssetBundleHeader bh = new AssetBundleHeader(); bool bDefault = true; if (ForceSep || NeedSep) { bh.option |= AssetBundleHeader.BundleOption.SepFile; } if (realass is Material) { bh.specialType = AssetBundleHeader.BundleSpecialType.Material; Material mt = realass as Material; if (mt.shader != null) { bDefault = false; List <string> pnames = new List <string>(); int nPCount = ShaderUtil.GetPropertyCount(mt.shader); for (int n = 0; n < nPCount; n++) { ShaderUtil.ShaderPropertyType spt = ShaderUtil.GetPropertyType(mt.shader, n); if (spt == ShaderUtil.ShaderPropertyType.TexEnv) { string pn = ShaderUtil.GetPropertyName(mt.shader, n); pnames.Add(pn); } } List <AssetBundleHeader.DepInfo> deplist = new List <AssetBundleHeader.DepInfo>(); foreach (var realsub in realsubs) { bool findtex = false; foreach (var texname in pnames) { Texture tex = mt.GetTexture(texname); if (tex) { string texpath = AssetDatabase.GetAssetPath(tex); if (!string.IsNullOrEmpty(texpath)) { string realpath = ConvertFileName(texpath) + ".u3dext"; realpath = realpath.ToLower(); if (realpath == realsub) { AssetBundleHeader.DepInfo info = new AssetBundleHeader.DepInfo(); info.name = texname; info.path = realsub; deplist.Add(info); findtex = true; } } } } if (!findtex) { AssetBundleHeader.DepInfo info = new AssetBundleHeader.DepInfo(); info.name = ""; info.path = realsub; deplist.Add(info); } } bh.deps = deplist.ToArray(); } } else if (realass is Texture) { bh.option |= AssetBundleHeader.BundleOption.ManuallyResolve; bh.specialType = AssetBundleHeader.BundleSpecialType.Texture; } else if (realass is Shader) { bh.option |= AssetBundleHeader.BundleOption.DonotRelease; } else if (realass is MonoScript) { bh.option |= AssetBundleHeader.BundleOption.DonotRelease; } else if (realass is Font) { bh.option |= AssetBundleHeader.BundleOption.DonotRelease; } if (bDefault) { bh.deps = new AssetBundleHeader.DepInfo[realsubs.Count]; for (int n = 0; n < realsubs.Count; n++) { bh.deps[n].name = ""; bh.deps[n].path = realsubs[n]; } } bh.Save(fs); fs.Write(content, 0, content.Length); } File.Delete(outfile); } }