using System; using System.Collections.Generic; using System.Linq; using HybridCLR.Editor.Commands; using HybridCLR.Editor.Installer; using UnityEditor; using UnityEditor.AddressableAssets.Settings; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.Windows; using BuildTarget = UnityEditor.BuildTarget; using Debug = UnityEngine.Debug; class BuildLauncher { private const string HybirdCLRGenerateDir = "/HybridCLRGenerate"; private const string HybirdCLRGenerateLinkPath = "Assets/HybridCLRGenerate/link.xml"; private const string GameLauncherSceneName = "Assets/Scenes/GameLauncher.unity"; private const string RemoteUser = "root"; private const string RemoteHost = "132.232.113.98"; private const string RemotePath = "/var/www/html/nld/{0}/{1}/{2}"; public static void BuildHotUpdateDlls() { var controller = new InstallerController(); if(!controller.HasInstalledHybridCLR()) controller.InstallDefaultHybridCLR(); // VersionControlSystem.Checkout(HybirdCLRGenerateDir); BuildLauncherBase.SetFileReadable(Application.dataPath + HybirdCLRGenerateDir); PrebuildCommand.GenerateAll(); HotUpadeEditorHelper.CopyHotUpdateDll(); HotUpadeEditorHelper.CopyMetaDataDll(); } public static void SetEnableHotUpdate(bool isEnable) { Debug.Log($"是否开启热更新:{isEnable}"); var gameLauncherScene = EditorSceneManager.OpenScene(GameLauncherSceneName, OpenSceneMode.Additive); if (!gameLauncherScene.IsValid()) { Debug.LogError($"无法加载场景{GameLauncherSceneName}"); return; } var gameLauncher = UnityEngine.Object.FindObjectOfType(); if (gameLauncher == null) { Debug.LogError("找不到GameLauncher"); return; } gameLauncher.enableHotUpdate = isEnable; EditorSceneManager.MarkSceneDirty(gameLauncherScene); EditorSceneManager.SaveScene(gameLauncherScene); EditorSceneManager.CloseScene(gameLauncherScene, false); HybridCLR.Editor.SettingsUtil.Enable = isEnable; if (!isEnable) { //删除HybirdCLR生成的link文件 AssetDatabase.DeleteAsset(HybirdCLRGenerateLinkPath); } } public static bool BuildAddressables(BuildTarget buildTarget, string profileName, string version, string workSpace, bool ignoreHotUpdate, bool needClearCached = false, List groups = null) { EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(buildTarget), buildTarget); SetEnableHotUpdate(!ignoreHotUpdate); if (!ignoreHotUpdate) { BuildHotUpdateDlls(); } return BuildLauncherBase.BuildAddressables(buildTarget, profileName, version, workSpace, needClearCached, groups, RemoteUser, RemoteHost, RemotePath); } public static void UpdatePreviousBuild(string profileName, bool ignoreHotUpdate, BuildTarget buildTarget, string workSpace) { EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(buildTarget), buildTarget); if (!ignoreHotUpdate) { BuildHotUpdateDlls(); } BuildLauncherBase.UpdatePreviousBuild(profileName, buildTarget, workSpace, RemoteUser, RemoteHost, RemotePath); } public static void BuildAddressablesAndPlayer(BuildTarget buildTarget, string profileName, string version, string workSpace, bool ignoreHotUpdate, bool needClearCached = false, bool isDev = false) { if (BuildAddressables(buildTarget, profileName, version, workSpace, ignoreHotUpdate, needClearCached)) { BuildLauncherBase.BuildPlayer(buildTarget,isDev); } } public static void BuildPlayer(BuildTarget buildTarget,bool isDev = false) { BuildLauncherBase.BuildPlayer(buildTarget,isDev); } } internal class BatchBuild { /// /// 批处理构建Addressables /// public static void BuildAddressables() { var enableHotUpdate = BatchBuildBase.Init(); if (enableHotUpdate) BuildLauncher.BuildHotUpdateDlls(); BatchBuildBase.BuildAddressables(); } /// /// 批处理构建Addressables和Player /// public static void BuildContentAndPlayer() { try { // 判断本地是否有define_list.txt文件 var defineListPath = Application.dataPath + "/../define_list.txt"; var appendDefineList = new List(); if (File.Exists(defineListPath)) { var defineList = System.IO.File.ReadAllLines(defineListPath); foreach (var define in defineList) { if (string.IsNullOrEmpty(define)) continue; appendDefineList.Add(define); } } var currentTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget); var oldDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentTargetGroup); // 判断是否有需要追加的宏 if (appendDefineList.Count > 0) { var newDefineList = new List(); newDefineList.AddRange(oldDefines.Split(';')); for (var i = 0; i < newDefineList.Count; i++) { if (string.IsNullOrEmpty(newDefineList[i])) continue; if (appendDefineList.Contains(newDefineList[i])) appendDefineList.Remove(newDefineList[i]); } newDefineList.AddRange(appendDefineList); PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTargetGroup, string.Join(";", newDefineList)); } var enableHotUpdate = BatchBuildBase.Init(); BuildLauncher.SetEnableHotUpdate(enableHotUpdate); if (enableHotUpdate) { BuildLauncher.BuildHotUpdateDlls(); } BatchBuildBase.BuildContentAndPlayer(); // 还原宏 PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTargetGroup, oldDefines); } catch (Exception e) { //Build Exception用于在日志文件中定位此次打包的异常 Debug.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}"); throw; } } /// /// 批处理增量更新资源 /// public static void UpdatePreviousBuild() { try { var enableHotUpdate = BatchBuildBase.Init(); if (enableHotUpdate) BuildLauncher.BuildHotUpdateDlls(); BatchBuildBase.UpdatePreviousBuild(); } catch (Exception e) { //Build Exception用于在日志文件中定位此次打包的异常 Debug.LogError($"[Build Exception]:{e.Message} \r\n {e.StackTrace}"); throw; } } }