/// <summary> /// 获取游戏中所有资源的assetbundle 信息,用来打包 /// </summary> /// <param name="config"></param> /// <returns></returns> public static void GenResBuild(BuilderConfig config) { // List<AssetBundleBuild> bundles = new List<AssetBundleBuild>(); string path = config.LoadPath; GenResBuildByDir(path, "*.*"); }
/// <summary> /// 获取游戏中所有Lua文件的assetbundle 信息,用来打包 /// </summary> /// <param name="config"></param> /// <returns></returns> public static void GenLuaBuild(BuilderConfig config) { //获取所有lua文件的bundle信息,所有lua文件需以.bytes为后缀名,否则不能打进assetbundle // 清空临时lua文件夹,正常情况下是不会有该文件夹的 if (Directory.Exists(sTempLuaDir)) { Directory.Delete(sTempLuaDir, true); } Directory.CreateDirectory(sTempLuaDir); //拷贝(编译)lua文件到临时目录 string[] srcDirs = { CustomSettings.lfuLuaDir , CustomSettings.baseLuaDir , Tools.GetLuaSrcPath() , Tools.GetGFLuaPath() }; foreach (var dir in srcDirs) { if (GameConfig.EncodeLua) { string sourceDir = dir; string[] files = Directory.GetFiles(sourceDir, "*.lua", SearchOption.AllDirectories); int len = sourceDir.Length; if (sourceDir[len - 1] == '/' || sourceDir[len - 1] == '\\') { --len; } for (int j = 0; j < files.Length; j++) { string str = files[j].Remove(0, len); string dest = sTempLuaDir + str + ".bytes"; string _dir = Path.GetDirectoryName(dest); Directory.CreateDirectory(_dir); EncodeLuaFile(files[j], dest, config); } } else { ToLuaMenu.CopyLuaBytesFiles(dir, sTempLuaDir); } } _refreshAssetDb(); while (EditorApplication.isUpdating) { } //通过GenLuaBuildByDir配置AssetBundle name foreach (var luaDir in Directory.GetDirectories(sTempLuaDir, "*", SearchOption.AllDirectories)) { GenLuaBuildByDir(luaDir, "*.bytes"); //Debug.LogWarning("lua Add luaDir:" + luaDir); } GenLuaBuildByDir(sTempLuaDir, "*.bytes"); //Debug.LogWarning("lua Add luaDir: lua/lua"); _refreshAssetDb(); }
public static void BuildAbsByConfig(BuilderConfig config) { if (BuildTarget.NoTarget == config.target) { BuildAsb(config.ExportPath, config.options, BuildTarget.Android); BuildAsb(config.ExportPath, config.options, BuildTarget.iOS); BuildAsb(config.ExportPath, config.options, BuildTarget.StandaloneWindows); } else { BuildAsb(config.ExportPath, config.options, config.target); } }
/// <summary> /// 按平台打包所有lua文件 /// </summary> /// <param name="config"></param> public static void BuildAllLua(BuilderConfig config) { mConfig = config; //根据选择平台打包所有lua文件 GenLuaBuild(config); BuildAbsByConfig(config); ////删除生成的Lua文件夹 //if (Directory.Exists(sTempLuaDir)) //{ // Directory.Delete(sTempLuaDir, true); //} }
private void Init() { //titleContent = new GUIContent("Builder"); minSize = new Vector2(300f, 400f); /** * 空合并运算符(??): * 用于定义可空类型和引用类型的默认值。如果此运算符的左操作数不为null,则此运算符将返回左操作数,否则返回右操作数。 * 例如:a??b 当a为null时则返回b,a不为null时则返回a本身。 * 空合并运算符为右结合运算符,即操作时从右向左进行组合的。如,“a??b??c”的形式按“a??(b??c)”计算 **/ _config = _config ?? new BuilderConfig(); GameConfig.Load(); }
/// <summary> /// 按平台打包所有lua文件和资源 /// </summary> /// <param name="config"></param> public static void BuildAll(BuilderConfig config) { mConfig = config; // 配置lua打包信息 GenLuaBuild(config); //配置所有资源打包信息 GenResBuild(config); //根据选择平台打包 BuildAbsByConfig(config); ////删除生成的Lua文件夹 //if (Directory.Exists(sTempLuaDir)) //{ // Directory.Delete(sTempLuaDir, true); //} }
public static void GenResBuildByDir(string dir, string pattern) { string relative2 = sResDir; if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir)) { //查看该文件夹下是否有文件,有文件将该文件夹作为一个bundle string[] files = Directory.GetFiles(dir, pattern, SearchOption.TopDirectoryOnly); int count = 0; foreach (var f in files) { if (BuilderConfig.IsResFile(f)) { count += AsbNameSetting.GetHasAsbName(f) ? 0 : 1; break; } } if (count > 0) { files = Directory.GetFiles(dir, pattern, SearchOption.AllDirectories); string asbName = Tools.GetAsbName(Tools.RelativeTo(dir, relative2)); // List<string> list = new List<string>(); for (int i = 0; i < files.Length; i++) { string f = files[i]; if (BuilderConfig.IsResFile(f)) { string resPath = Tools.RelativeTo(f, Application.dataPath, true); AsbNameSetting.SetBundleName(resPath, asbName, true); } } } else { //如果文件夹下没有文件,遍历其子文件夹 string[] dirs = Directory.GetDirectories(dir); if (dirs.Length > 0) { foreach (var d in dirs) { GenResBuildByDir(d, pattern); } } } } }
public static void EncodeLuaFile(string srcFile, string outFile, BuilderConfig config) { if (!srcFile.ToLower().EndsWith(".lua", StringComparison.Ordinal)) { File.Copy(srcFile, outFile, true); return; } bool isWin = true; string luaexe = string.Empty; string args = string.Empty; string exedir = string.Empty; string currDir = Directory.GetCurrentDirectory(); string platStr = "/"; if (BuildTarget.Android == config.target) { platStr = "_32/"; //Debug.LogError(platStr); } if (Application.platform == RuntimePlatform.WindowsEditor) { isWin = true; luaexe = "luajit.exe"; args = "-b -g " + srcFile + " " + outFile; exedir = Application.dataPath.Replace("Assets", "") + "LuaEncoder/luajit" + platStr; } else if (Application.platform == RuntimePlatform.OSXEditor) { isWin = false; luaexe = "./luajit"; args = "-b -g " + srcFile + " " + outFile; exedir = Application.dataPath.Replace("Assets", "") + "LuaEncoder/luajit_mac" + platStr; } Directory.SetCurrentDirectory(exedir); System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.FileName = luaexe; info.Arguments = args; info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; info.UseShellExecute = isWin; info.ErrorDialog = true; LogFile.Log(info.FileName + " " + info.Arguments); System.Diagnostics.Process pro = System.Diagnostics.Process.Start(info); pro.WaitForExit(); Directory.SetCurrentDirectory(currDir); }
public static void GenLuaBuildByDir(string dir, string pattern) { if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir)) { string relative2 = sTempLuaDir; string[] files = Directory.GetFiles(dir, pattern, SearchOption.TopDirectoryOnly); string asbName = "lua/" + Tools.GetAsbName(Tools.RelativeTo(dir, relative2, true)); for (int i = 0; i < files.Length; i++) { string f = files[i]; if (BuilderConfig.IsResFile(f)) { AsbNameSetting.SetBundleName(Tools.RelativeTo(files[i], Application.dataPath, true), asbName, true); } } } }
/// <summary> ///设置资源的AssetBundle Name /// </summary> /// <param name="resPath">以Assets/开头的相对文件路径</param> /// <param name="bundleName">AssetBundle Name.</param> /// <param name="skipIfHas">If set to <c>true</c> 跳过已经设置AssetBundle name的</param> public static void SetBundleName(string resPath, string bundleName, bool skipIfHas = false) { if (BuilderConfig.IsResFile(resPath)) { AssetImporter importer = AssetImporter.GetAtPath(resPath); if (importer && importer.assetBundleName != bundleName) { if (skipIfHas && !string.IsNullOrEmpty(importer.assetBundleName)) { //已经有Assetbundle name, 跳过 return; } importer.assetBundleName = bundleName; Debug.LogFormat("设置 Assetbundle 名称:{0} -----> {1}", resPath, bundleName); } } }
//[MenuItem("Assets/打包测试")] //public static void BuildAll() //{ // string outDir = Application.dataPath.Replace("Assets", "AssetBundles/test"); // Debug.Log("打包路径:" + outDir); // Tools.CheckDirExists(outDir, true); // BuildPipeline.BuildAssetBundles(outDir, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget); //} private static void _setBundleNameByPath(string path) { string fullPath = path; //if (!Path.IsPathRooted(path) && !path.StartsWith(Application.dataPath, StringComparison.Ordinal)) if (!path.StartsWith(Application.dataPath, StringComparison.Ordinal)) { fullPath = Tools.PathCombine(Directory.GetParent(Application.dataPath).ToString(), path); } else { path = Tools.RelativeTo(path, Tools.GetResPath(), true); } if (File.Exists(fullPath)) { if (BuilderConfig.IsResFile(path)) { string asbName = fullPath.Substring(0, fullPath.Length - Path.GetExtension(fullPath).Length); //asbName = Tools.RelativeTo(asbName, Tools.GetResPath()) + GameConfig.STR_ASB_EXT; asbName = Tools.GetAsbName(Tools.RelativeTo(asbName, Tools.GetResPath())); SetBundleName(path, asbName.ToLower()); } } else if (Directory.Exists(fullPath)) { string[] files = Directory.GetFiles(fullPath, "*.*", SearchOption.AllDirectories); //string asbName = Tools.RelativeTo(fullPath, Tools.GetResPath()) + GameConfig.STR_ASB_EXT; string asbName = Tools.GetAsbName(Tools.RelativeTo(fullPath, Tools.GetResPath())); foreach (var f in files) { if (BuilderConfig.IsResFile(f)) { string assetPath = Tools.RelativeTo(f, Application.dataPath, true); SetBundleName(assetPath, asbName.ToLower()); } } } }
/// <summary> /// 按平台打包所有资源代码 /// </summary> /// <param name="config"></param> public static void BuildAllRes(BuilderConfig config) { mConfig = config; GenResBuild(config); BuildAbsByConfig(config); }
void OnGUI() { if (this._config == null) { Init(); } EditorGUILayout.BeginHorizontal(); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _config.LoadPath = EditorGUILayout.TextField("Load Path", _config.LoadPath); if (GUILayout.Button("Select")) { var path = EditorUtility.OpenFolderPanel("Load", _config.LoadPath, ""); _config.LoadPath = string.IsNullOrEmpty(path) ? _config.LoadPath : path; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _config.ExportPath = EditorGUILayout.TextField("Export Path", _config.ExportPath); if (GUILayout.Button("Select")) { var path = EditorUtility.OpenFolderPanel("Load", _config.ExportPath, ""); _config.ExportPath = string.IsNullOrEmpty(path) ? _config.ExportPath : path; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("BuildAssetBundleOptions:"); if (GUILayout.Button(_config.options.ToString())) { ShowTypeNamesMenu( _config.options.ToString(), optionsList, (string selectedTypeStr) => { _config.options = getEnumByString <BuildAssetBundleOptions>(selectedTypeStr); } ); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("BuildTarget:"); if (GUILayout.Button(_config.target.ToString())) { ShowTypeNamesMenu( _config.target.ToString(), targetList, (string selectedTypeStr) => { _config.target = getEnumByString <BuildTarget>(selectedTypeStr); } ); } EditorGUILayout.EndHorizontal(); if (GUILayout.Button("重置资源导出路径")) { _config.DeletConfig(); _config = new BuilderConfig(); } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("LuaBuildTest")) { //List<AssetBundleBuild> list = new List<AssetBundleBuild>(); ////测试常规打包载入 //list.Add(AsbBuilder.GenBuildByDir(_config.LoadPath, Tools.GetResPath(), "*.*")); ////更新相关测试 begin ------------------------------------------------ ////测试资源依赖 ////测试资源依赖:1. 多个文件依赖同一个文件 //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t1/common.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T1/cmat.mat", "Assets/BundleRes/DepTest/T1/img.png", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t1/sphere.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T1/Sphere.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t1/cube.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T1/Cube.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t1/capsule.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T1/Capsule.prefab", } }); ////测试资源依赖:2. 一个文件依赖多个文件 ////list = new List<AssetBundleBuild>(); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t2/obj.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T2/Obj.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t2/sphere.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T2/Sphere.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t2/cube.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T2/Cube.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t2/capsule.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T2/Capsule.prefab", } }); ////测试资源依赖:3. 一列依赖对象中有变化 ////list = new List<AssetBundleBuild>(); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t3/cube.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T3/Cube.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t3/img2.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T3/img2.png", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t3/mat_img2.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T3/mat_img2.mat", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t3/obj2.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T3/Obj2.prefab", } }); ////测试资源依赖:4.对有c#脚本的绑定的依赖 //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t4/sphere.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T4/Sphere.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t4/cube.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T4/Cube.prefab", } }); //list.Add(new AssetBundleBuild() { assetBundleName = "deptest/t4/capsule.asb", assetNames = new string[] { "Assets/BundleRes/DepTest/T4/Capsule.prefab", } }); ////更新相关测试 end ================================================== //if (BuildTarget.NoTarget == _config.target) //{ // AsbBuilder.BuildAsb(_config.ExportPath, list.ToArray(), _config.options, BuildTarget.Android); // AsbBuilder.BuildAsb(_config.ExportPath, list.ToArray(), _config.options, BuildTarget.iOS); // AsbBuilder.BuildAsb(_config.ExportPath, list.ToArray(), _config.options, BuildTarget.StandaloneWindows); //} //else //{ // AsbBuilder.BuildAsb(_config.ExportPath, list.ToArray(), _config.options, _config.target); //} AsbBuilder.BuildAllLua(_config); Close(); } if (GUILayout.Button("ResBuildTest")) { AsbBuilder.BuildAllRes(_config); Close(); } EditorGUILayout.EndHorizontal(); if (GUILayout.Button("BuildAll")) { AsbBuilder.BuildAll(_config); Close(); } EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); bool v = GUILayout.Toggle(GameConfig.HasDebugView, "是否显示DebugView"); if (v != GameConfig.HasDebugView) { GameConfig.HasDebugView = v; GameConfig.Save(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("设置清理不打包的 Scenens ")) { SceneSettingManager.SetReleaseSceneToBuildSetting(); } if (GUILayout.Button("显示 BuildSettings")) { EditorApplication.ExecuteMenuItem("File/Build Settings..."); Close(); } EditorGUILayout.EndHorizontal(); }