/// <summary> /// 打包资源 (依赖关系) /// </summary> /// <param name="source">资源名称(No1_Res/)</param> /// <param name="abConfig"></param> static void fileWithDepends(string source, ref AssetbundleConfig abConfig) { string _source = Replace(source); if (_source == "") { return; } _source = _source.Substring(Application.dataPath.Length + 1); _source = "Assets/" + _source; string[] dps = AssetDatabase.GetDependencies(_source); foreach (var dp in dps) { if (dp.EndsWith(".cs")) { continue; } AssetImporter assetImporter = AssetImporter.GetAtPath(dp); string fullname = dp.Replace("Assets/ForAssetbundleMaking/", ""); fullname = fullname.Substring(fullname.IndexOf('/') + 1); //资源的名字 string bagname = fullname.Replace(Path.GetExtension(fullname), ".unity3d"); assetImporter.assetBundleName = bagname; //Debug.Log("bagna:" + bagname); abConfig.AddAssetBundle(fullname, bagname); //资源名和包名关系 } }
static void CreateBundleInfo(string game, AssetbundleConfig abinfo) { string path = pathConfig_Editor + "/" + game + "/bundle.config"; string str = abinfo.ToString(); FileUtil.SaveFile(path, abinfo.ToString()); CopyToStreamingAssets(game, path); }
void initAssetManager() { string[] strs = System.Enum.GetNames(typeof(NEngine.Game.GameEnum)); for (int i = 0; i < strs.Length; i++) { AssetVersion ver1 = AssetVersion.FromString(FileUtil.ReadFromFile(appConfigPath_old + "/Conn/" + strs[i] + "/assetVersion.config")); AssetbundleConfig ver2 = AssetbundleConfig.FromString(FileUtil.ReadFromFile(appConfigPath_old + "/Conn/" + strs[i] + "/abConfig.config")); PreloadConfig ver3 = PreloadConfig.FromString(FileUtil.ReadFromFile(appConfigPath_old + "/Conn/" + strs[i] + "/preloadConfig.config")); App.GetMgr <AssetManager>().AddAssetConfig(strs[i], new AssetConfig(strs[i], ver1, ver2, ver3)); } }
static void BuildSourceToAB(string game) { ClearAssetBundlesName(); AssetbundleConfig abconfig = new AssetbundleConfig(); Pack(game, ref abconfig); string outputPath = pathRes_Editor + "/" + game + "_ab"; //生成的ab包路径 DeleteDirectory(outputPath); Directory.CreateDirectory(outputPath); Debug.Log("BuildSourceToAB outputPath = " + outputPath); BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);//会在根目录生成manifest //Debug.Log("打包完成"); ////生成Config文件 ClearConfigStreamAssets(game); CreatAssetversionInfo(game); CreateBundleInfo(game, abconfig); CreatePreloadInfo(game); }
/// <summary> /// 打包 不处理依赖关系 /// </summary> /// <param name="source"></param> static void file(string source, ref AssetbundleConfig abconfig) { string _source = Replace(source); if (_source == "") { return; } _source = _source.Substring(Application.dataPath.Length + 1); _source = "Assets/" + _source; AssetImporter assetImport = AssetImporter.GetAtPath(_source); string fullname = _source.Replace("Assets/ForAssetbundleMaking/", ""); fullname = fullname.Substring(fullname.IndexOf('/') + 1); //资源的名字 string bagname = fullname.Replace(Path.GetExtension(fullname), ".unity3d"); assetImport.assetBundleName = bagname; //Debug.Log("bagna:" + bagname); abconfig.AddAssetBundle(fullname, bagname); //资源名和包名关系 }
static void Pack(string source, bool isDetailDepends, ref AssetbundleConfig abconfig) { Debug.Log("source: " + source); DirectoryInfo folder = null; FileSystemInfo[] files = null; try { folder = new DirectoryInfo(source); files = folder.GetFileSystemInfos(); } catch { Debug.LogWarning("no files at the folder:" + source); return; } int length = files.Length; for (int i = 0; i < length; i++) { if (files[i] is DirectoryInfo) { Pack(files[i].FullName, isDetailDepends, ref abconfig); } else { if (!files[i].Name.EndsWith(".meta")) { if (files[i].Name.EndsWith(".cs"))// || files[i].Name.Equals("assetVersionInfo.txt")) { continue; } if (isDetailDepends) { fileWithDepends(files[i].FullName, ref abconfig); } else { file(files[i].FullName, ref abconfig); } } } } }
void initAppConfig() { string[] gs = System.Enum.GetNames(typeof(GameEnum)); APPVersion app = new APPVersion(); //AssetConfig for (int i = 0; i < gs.Length; i++) { GameVersion ver = new GameVersion(gs[i], "name:" + gs[i]); app.Add(ver); AssetVersion aver = new AssetVersion(); aver.assetVersion = 0; AssetbundleConfig abcon = new AssetbundleConfig(); abcon.AddAssetBundle("test", "has001"); PreloadConfig pre = new PreloadConfig(); FileUtil.SaveFile(path + "/" + gs[i] + "/assetVersion.config", aver.ToStr()); FileUtil.SaveFile(path + "/" + gs[i] + "/abConfig.config", abcon.ToString()); FileUtil.SaveFile(path + "/" + gs[i] + "/preloadConfig.config", pre.ToString()); } FileUtil.SaveFile(path + "/appVersion.config", app.ToString()); //appversion }
protected AssetConfig LoadLocalConfig(string game) { Debug.Log("资源配置文件loading"); string path = Application.streamingAssetsPath + "/CONFIG/" + game; string[] files = System.IO.Directory.GetFiles(path); AssetbundleConfig bundle = null; AssetVersion assetversion = null; PreloadConfig preload = null; string str = ""; string name = ""; for (int i = 0; i < files.Length; i++) { str = ""; using (System.IO.StreamReader sr = new System.IO.StreamReader(files[i])) { name = Util.GetFileName(files[i]); str = sr.ReadToEnd(); //str = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.UTF8.GetBytes(str)); if (name == "bundle.config") { bundle = AssetbundleConfig.FromString(str); } else if (name == "assetVersion.config") { assetversion = AssetVersion.FromString(str); } else if (name == "preload.config") { preload = PreloadConfig.FromString(str); } sr.Close(); } } return(new AssetConfig(game, assetversion, bundle, preload)); }
static void Pack(string game, ref AssetbundleConfig abconfig) { Pack(pathPacking + "/ForPackWithDependices/" + game + "_Res", true, ref abconfig); //处理依赖关系打包方式 Pack(pathPacking + "/ForPackWithNoDependices/" + game + "_Res", false, ref abconfig); //不处理依赖关系打包 }