using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using System.Linq; using GameEditor.Core; using GameEditor.GameEditor.PlayerBuilder; using GameLogic; using System.Diagnostics; using ResUpdate; namespace GameEditor.FrameTool { public class BuildWindow : EditorWindow { /// /// 版本文件名 /// const string VersionsFile = "version"; Version version; string defaultVersion; string subChannel; string serverUrl; string resUrl; /// /// 导出APK/XCODE工程 /// bool isBuildPlayer; /// /// 是否拷贝AB包到StreamingAssets /// bool isCopyABSToStreamingAssets; // Add menu named "My Window" to the Window menu [MenuItem("Build/BuildWindow")] static void Init() { // Get existing open window or if none, make a new one: BuildWindow window = (BuildWindow)EditorWindow.GetWindow(typeof(BuildWindow)); window.Show(); window.InitWindow(); } void InitWindow() { InitSize(); InitGames(); } /// /// 初始化大小 /// void InitSize() { minSize = new Vector2(300, 400); maxSize = new Vector2(300, 650); } /// /// 初始化游戏 /// void InitGames() { version = new Version(Resources.Load(VersionsFile).text); defaultVersion = version.GetInfo("version"); subChannel = version.GetInfo("subChannel"); serverUrl = version.GetInfo("serverUrl"); resUrl = version.GetInfo("resUrl"); } void OnGUI() { EditorGUILayout.BeginVertical(); EditorGUILayout.Space(); EditorGUILayout.LabelField(string.Format("当前平台:{0}", EditorUserBuildSettings.activeBuildTarget.ToString())); EditorGUILayout.Space(); EditorGUILayout.EndVertical(); isCopyABSToStreamingAssets = EditorGUILayout.BeginToggleGroup("拷贝AssetBundle到流媒体目录(打包到App包体内)", isCopyABSToStreamingAssets); if (isCopyABSToStreamingAssets) { EditorGUILayout.BeginVertical(); defaultVersion = EditorGUILayout.TextField("Version", defaultVersion); subChannel = EditorGUILayout.TextField("SubChannel", subChannel); serverUrl = EditorGUILayout.TextField("ServerUrl", serverUrl); resUrl = EditorGUILayout.TextField("ResUrl", resUrl); EditorGUILayout.EndVertical(); } EditorGUILayout.EndToggleGroup(); EditorGUILayout.Space(); isBuildPlayer = EditorGUILayout.BeginToggleGroup(GetBuildTitle(), isBuildPlayer); if (isBuildPlayer) { var gameSet = GameObject.FindObjectOfType(); gameSet.settingInfo.isUpdate = true; gameSet.settingInfo.bundleMode = true; gameSet.settingInfo.isDebug = false; gameSet.settingInfo.luaBundleMode = true; } EditorGUILayout.EndToggleGroup(); EditorGUILayout.Space(); if (GUILayout.Button("整体打包", GUILayout.Height(40f))) { if (EditorUtility.DisplayDialog("打包提示", "打包将持续一段时间,确定打包?", "是", "否")) //显示对话框 { BuildGame(); } } if (GUILayout.Button("单打lua资源", GUILayout.Height(40f))) { if (EditorUtility.DisplayDialog("打包提示", "单打lua资源将持续一段时间,确定打包?", "是", "否")) //显示对话框 { if (FrameTool.BuildLuaAssetBundles()) { //拷贝AssetBundle到流媒体目录 if (isCopyABSToStreamingAssets) { string exportPath = AssetBundle.AssetBundleConfig.GetExportPath(EditorUserBuildSettings.activeBuildTarget); string targetPath = FrameTool.GetStreamingAssetPath(EditorUserBuildSettings.activeBuildTarget); File.Copy(exportPath + "/lzma/luabytes.unity3d", targetPath + "/lzma/luabytes.unity3d", true); File.Copy(exportPath + "/lzma/resconfigs.unity3d", targetPath + "/lzma/resconfigs.unity3d", true); File.Copy(exportPath + "/files.unity3d", targetPath + "/files.unity3d", true); SaveVersionFile(); } Close(); } } } if (GUILayout.Button("资源合法性检查", GUILayout.Height(40f))) { FrameTool.CheckAssetBundleStates(); } if (GUILayout.Button("查看多依赖资源", GUILayout.Height(40f))) { FrameTool.AssetsDuplicatedInMultBundlesCache(1); } if (GUILayout.Button("处理多依赖资源", GUILayout.Height(40f))) { FrameTool.AssetsDuplicatedInMultBundlesCache(2); } } /// /// 打包游戏 /// void BuildGame() { //打包游戏 FrameTool.BuildGameAssetBundles(); //拷贝AssetBundle到流媒体目录 if (isCopyABSToStreamingAssets) { FrameTool.CopyAssetBundleToStreamingAssets(); SaveVersionFile(); } //是否BuildPlayer if (isBuildPlayer) { switch (EditorUserBuildSettings.activeBuildTarget) { case BuildTarget.Android: ExportApk(); break; case BuildTarget.iOS: ExportXcode(); break; } } Close(); } void SaveVersionFile() { var VersionsFilePath = Application.dataPath + "/Resources/" + VersionsFile + ".txt"; var VersionsFilePath2 = AppConst.PersistentDataPath + VersionsFile + ".txt"; version.SetInfo("version", defaultVersion); version.SetInfo("subChannel", subChannel); version.SetInfo("serverUrl", serverUrl); version.SetInfo("resUrl", resUrl); string directoryPath = Path.GetDirectoryName(VersionsFilePath); if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); File.WriteAllText(VersionsFilePath, version.ToJson(), System.Text.Encoding.UTF8); string directoryPath2 = Path.GetDirectoryName(VersionsFilePath2); if (!Directory.Exists(directoryPath2)) Directory.CreateDirectory(directoryPath2); File.WriteAllText(VersionsFilePath2, version.ToJson(), System.Text.Encoding.UTF8); } /// /// 获取是否打包 /// /// string GetBuildTitle() { switch (EditorUserBuildSettings.activeBuildTarget) { case BuildTarget.Android: return "导出APK"; case BuildTarget.iOS: return "导出XCODE工程"; } return "请切换到Android/IOS平台"; } /// /// 导出XCODE工程 /// void ExportXcode() { PlayerBuilder.BuildXCode(); } /// /// 是否导出APK /// void ExportApk() { PlayerBuilder.BuildApk(); } } }