/* * @Author: 白哉 * @Description: * @Date: 2025年07月25日 星期五 14:07:55 * @Modify: */ using System.Linq; using Logic.Config; using UnityEditor; using UnityEngine; namespace Logic.Editor { public class BuildEditor : EditorWindow { // 定义宏名称 public const string ENABLE_SPEEDUP = "ENABLE_SPEEDUP"; public const string GAME_AUTO_DEBUG = "GAME_AUTO_DEBUG"; public const string CHECK_ACTIONDEFFERENCE = "CHECK_ACTIONDEFFERENCE"; public const string STEAM_TEST = "STEAM_TEST"; public const string STEAM_CHANNEL = "STEAM_CHANNEL"; public const string USE_INPUT = "USE_INPUT"; private VersionConfig _asset; private uint _major; private uint _minor; private uint _patch; private uint _fourth; private int _index; // 背景 private GUIStyle _redBoxStyle; private GUIStyle _whiteBoxStyle; [MenuItem("Tools/打包工具")] private static void ShowWindow() { var window = GetWindow(); window.titleContent = new GUIContent("打包工具"); window.Show(); } private void OnEnable() { } private void OnGUI() { if (_redBoxStyle == null) { _redBoxStyle = InspectorUtils.GetHelpBoxStyle(); InspectorUtils.AddBorder(_redBoxStyle, new Color(0.5f, 0.4f, 0.4f, 0.6f)); } if (_whiteBoxStyle == null) { _whiteBoxStyle = InspectorUtils.GetHelpBoxStyle(); InspectorUtils.AddBorder(_whiteBoxStyle, new Color(1f, 1f, 1f, 0.2f)); } if (!_asset) { var path = $"Assets/BundleResources/DataAssets/VersionConfig.asset"; _asset = AssetDatabase.LoadAssetAtPath(path); if (!_asset) { _asset = CreateInstance(); AssetDatabase.CreateAsset(_asset, path); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } _asset.Versions = _asset.Versions.OrderByDescending(v => v.VersionId).ToList(); } EditorGUILayout.BeginHorizontal(); if (InspectorUtils.InspectorButtonWithTextWidth("保存")) { EditorUtility.SetDirty(_asset); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } // 所有已存在的版本号主版本号降低至0 if (InspectorUtils.InspectorButtonWithTextWidth("主版本号降低至0")) { foreach (var version in _asset.Versions) { version.MajorVersion = 0; _asset.Versions = _asset.Versions.OrderByDescending(v => v.VersionId).ToList(); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); InspectorUtils.InspectorTextWidthRich($"版本号: "); _major = (uint)EditorGUILayout.IntField((int)_major, GUILayout.Width(20)); _minor = (uint)EditorGUILayout.IntField((int)_minor, GUILayout.Width(20)); _patch = (uint)EditorGUILayout.IntField((int)_patch, GUILayout.Width(20)); _fourth = (uint)EditorGUILayout.IntField((int)_fourth, GUILayout.Width(20)); var versionId = _major * 1000000 + _minor * 10000 + _patch * 100 + _fourth; var desc = $"{_major}.{_minor}.{_patch}.{_fourth}"; if (_asset.GetVersionInfo(versionId) != null) { InspectorUtils.InspectorTextWidthRich($"已存在版本号{_major}.{_minor}.{_patch}.{_fourth}"); } else { if (InspectorUtils.InspectorButtonWithTextWidth($"创建版本号{desc}")) { _asset.CreateNewVersion(_major, _minor, _patch, _fourth); _asset.Versions = _asset.Versions.OrderByDescending(v => v.VersionId).ToList(); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (_asset.Versions.Count == 0) return; EditorGUILayout.BeginVertical(_redBoxStyle); InspectorUtils.InspectorTextWidthRich($"版本构建"); var versionList = _asset.Versions.Select(v => $"{v.FullVersion}").ToArray(); _index = Mathf.Clamp(_index, 0, versionList.Length - 1); _index = EditorGUILayout.Popup(_index, versionList); var selectedVersion = _asset.Versions[_index]; EditorGUILayout.BeginVertical(_whiteBoxStyle); InspectorUtils.InspectorTextWidthRich($"版本{selectedVersion.FullVersion}描述: "); selectedVersion.Description = EditorGUILayout.TextArea(selectedVersion.Description, GUILayout.Height(240)); EditorGUILayout.EndVertical(); EditorGUILayout.BeginHorizontal(); #if ENABLE_SPEEDUP InspectorUtils.InspectorTextWidthRich($"加速模式已开启:"); if (InspectorUtils.InspectorButtonWithTextWidth($"关闭加速模式")) RemoveDefine(ENABLE_SPEEDUP); #else InspectorUtils.InspectorTextWidthRich($"加速模式已关闭:"); if (InspectorUtils.InspectorButtonWithTextWidth($"开启加速模式")) AddDefine(ENABLE_SPEEDUP); #endif EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); #if GAME_AUTO_DEBUG InspectorUtils.InspectorTextWidthRich($"自动战斗已开启:"); if (InspectorUtils.InspectorButtonWithTextWidth($"关闭自动战斗")) RemoveDefine(GAME_AUTO_DEBUG); #else InspectorUtils.InspectorTextWidthRich($"自动战斗已关闭:"); if (InspectorUtils.InspectorButtonWithTextWidth($"开启自动战斗")) AddDefine(GAME_AUTO_DEBUG); #endif EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); #if CHECK_ACTIONDEFFERENCE InspectorUtils.InspectorTextWidthRich($"MapData数据变化检查已开启:"); if (InspectorUtils.InspectorButtonWithTextWidth($"关闭MapData数据变化检查")) RemoveDefine(CHECK_ACTIONDEFFERENCE); #else InspectorUtils.InspectorTextWidthRich($"MapData数据变化检查已关闭:"); if (InspectorUtils.InspectorButtonWithTextWidth($"开启MapData数据变化检查")) AddDefine(CHECK_ACTIONDEFFERENCE); #endif EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); #if STEAM_TEST InspectorUtils.InspectorTextWidthRich($"Steam测试窗口已开启:"); if (InspectorUtils.InspectorButtonWithTextWidth($"关闭Steam测试窗口")) RemoveDefine(STEAM_TEST); #else InspectorUtils.InspectorTextWidthRich($"Steam测试窗口已关闭:"); if (InspectorUtils.InspectorButtonWithTextWidth($"开启Steam测试窗口")) AddDefine(STEAM_TEST); #endif EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (InspectorUtils.InspectorButtonWithTextWidth($"构建测试包")) { AddDefine(STEAM_CHANNEL); AddDefine(USE_INPUT); _asset.CurVersionId = selectedVersion.VersionId; PlayerSettings.productName = $"TOHOTOPIA"; EditorUtility.SetDirty(_asset); AssetDatabase.SaveAssets(); // 更新Unity版本号 PlayerSettings.bundleVersion = selectedVersion.FullVersion; // Debug包配置 SetDebugBuildSettings(); // 开始构建 // BuildPipeline.BuildPlayer(GetBuildScenes(), // $"../Pack/Debug_{selectedVersion.FullVersion}/{Application.productName}.exe", // BuildTarget.StandaloneWindows64, // BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.ConnectWithProfiler); EditorUtility.SetDirty(_asset); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } if (InspectorUtils.InspectorButtonWithTextWidth($"构建发布包")) { AddDefine(STEAM_CHANNEL); RemoveDefine(ENABLE_SPEEDUP); RemoveDefine(GAME_AUTO_DEBUG); RemoveDefine(CHECK_ACTIONDEFFERENCE); RemoveDefine(STEAM_TEST); RemoveDefine(USE_INPUT); _asset.CurVersionId = selectedVersion.VersionId; PlayerSettings.productName = $"TOHOTOPIA"; EditorUtility.SetDirty(_asset); AssetDatabase.SaveAssets(); // 更新Unity版本号 PlayerSettings.bundleVersion = selectedVersion.FullVersion; // Release包配置 SetReleaseBuildSettings(); // // 开始构建 // BuildPipeline.BuildPlayer(GetBuildScenes(), // $"../Pack/Release_{selectedVersion.FullVersion}/{Application.productName}.exe", // BuildTarget.StandaloneWindows64, // BuildOptions.None); EditorUtility.SetDirty(_asset); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } private void SetDebugBuildSettings() { // 脚本后端 PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.Mono2x); // 开发者模式 EditorUserBuildSettings.development = true; // 允许debug EditorUserBuildSettings.allowDebugging = true; // 日志等级 PlayerSettings.SetStackTraceLogType(LogType.Log, StackTraceLogType.ScriptOnly); PlayerSettings.SetStackTraceLogType(LogType.Warning, StackTraceLogType.ScriptOnly); PlayerSettings.SetStackTraceLogType(LogType.Error, StackTraceLogType.ScriptOnly); PlayerSettings.SetStackTraceLogType(LogType.Assert, StackTraceLogType.ScriptOnly); PlayerSettings.SetStackTraceLogType(LogType.Exception, StackTraceLogType.ScriptOnly); // 开启详细日志 PlayerSettings.usePlayerLog = true; // 开启深度剖析器 PlayerSettings.enableInternalProfiler = true; SetObfuscationEnabled(false); } private void SetReleaseBuildSettings() { // 脚本后端 PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.IL2CPP); // 关闭开发者模式 EditorUserBuildSettings.development = false; // 关闭debug EditorUserBuildSettings.allowDebugging = false; // 日志等级(只显示错误和异常) PlayerSettings.SetStackTraceLogType(LogType.Log, StackTraceLogType.None); PlayerSettings.SetStackTraceLogType(LogType.Warning, StackTraceLogType.None); PlayerSettings.SetStackTraceLogType(LogType.Error, StackTraceLogType.ScriptOnly); PlayerSettings.SetStackTraceLogType(LogType.Assert, StackTraceLogType.ScriptOnly); PlayerSettings.SetStackTraceLogType(LogType.Exception, StackTraceLogType.ScriptOnly); // 关闭详细日志 PlayerSettings.usePlayerLog = false; // 关闭深度剖析器 PlayerSettings.enableInternalProfiler = false; // 开启OPS混淆 SetObfuscationEnabled(true); } private void SetObfuscationEnabled(bool enabled) { var trueStr = "Global_Enable_Obfuscation\",\r\n \"Value\" : \"True\""; var falseStr = "Global_Enable_Obfuscation\",\r\n \"Value\" : \"False\""; // 获取json文件路径 string jsonPath = "Assets/OPS/Obfuscator/Settings/Obfuscator_Settings.json"; // 读取json文件内容 string jsonContent = System.IO.File.ReadAllText(jsonPath); if (enabled) jsonContent = jsonContent.Replace(falseStr, trueStr); else jsonContent = jsonContent.Replace(trueStr, falseStr); // 保存修改后的json System.IO.File.WriteAllText(jsonPath, jsonContent); // 刷新资源 AssetDatabase.Refresh(); } public void AddDefine(string symbol) { string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone); if (!defines.Contains(symbol)) { defines = string.IsNullOrEmpty(defines) ? symbol : defines + ";" + symbol; PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, defines); } } public void RemoveDefine(string symbol) { string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone); if (defines.Contains(symbol)) { defines = defines.Replace($"{symbol};", "") .Replace($";{symbol}", "") .Replace(symbol, "") .Replace(";;", ";") .Trim(';'); PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, defines); } } public void AddAutoBattle() { string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone); if (!defines.Contains(GAME_AUTO_DEBUG)) { defines = string.IsNullOrEmpty(defines) ? GAME_AUTO_DEBUG : defines + ";" + GAME_AUTO_DEBUG; PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, defines); } } public void RemoveAutoBattle() { string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone); if (defines.Contains(GAME_AUTO_DEBUG)) { defines = defines.Replace($"{GAME_AUTO_DEBUG};", "") .Replace($";{GAME_AUTO_DEBUG}", "") .Replace(GAME_AUTO_DEBUG, "") .Replace(";;", ";") .Trim(';'); PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, defines); } } } }