增加打包工具

This commit is contained in:
wuwenbo 2025-07-30 18:02:19 +08:00
parent 51b7ea056a
commit c4883bd71d
7 changed files with 206 additions and 133 deletions

View File

@ -12,7 +12,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c659b850b20e460f866ed3f696be406b, type: 3}
m_Name: VersionConfig
m_EditorClassIdentifier:
majorVersion: 1
minorVersion: 0
patchVersion: 0
buildNumber: 22
CurVersionId: 10000
Versions:
- MajorVersion: 1
MinorVersion: 0
PatchVersion: 0
Description:

View File

@ -36,7 +36,7 @@ namespace Logic.Config
if (!VersionCfg)
{
VersionCfg = Resources.Load<VersionConfig>("DataAssets/VersionConfig");
VersionCfg = Resources.Load<VersionConfig>("Export/VersionConfig");
}
}

View File

@ -6,6 +6,9 @@
*/
using System;
using System.Collections.Generic;
using Logic.Multilingual;
using UnityEngine;
@ -13,11 +16,43 @@ namespace Logic.Config
{
public class VersionConfig : ScriptableObject
{
public string majorVersion; // 主版本号
public string minorVersion; // 次版本号
public string patchVersion; // 补丁号
public int buildNumber; // 构建号
public uint CurVersionId; // 当前版本ID
public List<VersionInfo> Versions = new List<VersionInfo>();
public VersionInfo CurVersionInfo => GetVersionInfo(CurVersionId);
public string FullVersion => $"{majorVersion}.{minorVersion}.{patchVersion}";
public void CreateNewVersion(uint major, uint minor, uint patch)
{
var newVersion = new VersionInfo
{
MajorVersion = major,
MinorVersion = minor,
PatchVersion = patch
};
Versions.Add(newVersion);
}
public VersionInfo GetVersionInfo(uint versionId)
{
if (Versions == null || Versions.Count == 0) return null;
foreach (var version in Versions)
{
if (version.VersionId == versionId) return version;
}
return null;
}
}
[Serializable]
public class VersionInfo
{
public uint MajorVersion; // 主版本号
public uint MinorVersion; // 次版本号
public uint PatchVersion; // 补丁号
[MultilingualField]
public string Description; // 版本描述
public uint VersionId => MajorVersion * 10000 + MinorVersion * 100 + PatchVersion; // 版本ID格式为 Major.Minor.Patch
public string FullVersion => $"{MajorVersion}.{MinorVersion}.{PatchVersion}";
}
}

View File

@ -7,18 +7,32 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Logic.Config;
using UnityEditor;
using UnityEngine;
using OPS.Obfuscator.Editor;
using OPS.Obfuscator.Editor.Settings;
using TMPro;
using UnityEngine.TextCore;
using UnityEngine.TextCore.LowLevel;
namespace Logic.Editor
{
public class BuildEditor : EditorWindow
{
// 定义宏名称
public const string GAME_AUTO_DEBUG = "GAME_AUTO_DEBUG";
private VersionConfig _asset;
private const string OUTPUT_PATH = "Build/StandaloneWindows64";
private uint _major;
private uint _minor;
private uint _patch;
private int _index;
// 背景
private GUIStyle _redBoxStyle;
private GUIStyle _whiteBoxStyle;
[MenuItem("Tools/打包工具")]
private static void ShowWindow()
@ -30,6 +44,22 @@ namespace Logic.Editor
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));
}
var path = $"Assets/Resources/DataAssets/VersionConfig.asset";
_asset = AssetDatabase.LoadAssetAtPath<VersionConfig>(path);
if (!_asset)
@ -39,134 +69,169 @@ namespace Logic.Editor
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
private void OnGUI()
{
_asset.majorVersion = EditorGUILayout.TextField("重大更新", _asset.majorVersion);
_asset.minorVersion = EditorGUILayout.TextField("功能更新", _asset.minorVersion);
_asset.patchVersion = EditorGUILayout.TextField("补丁修复", _asset.patchVersion);
if (GUILayout.Button($"构建 Debug 包"))
EditorGUILayout.BeginHorizontal();
InspectorUtils.InspectorTextWidthRich($"<b>版本号选择: </b>");
_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));
EditorGUILayout.EndHorizontal();
var versionId = _major * 10000 + _minor * 100 + _patch;
var desc = $"{_major}.{_minor}.{_patch}";
var versionInfo = _asset.GetVersionInfo(versionId);
if (versionInfo == null && InspectorUtils.InspectorButtonWithTextWidth($"创建版本号{desc}"))
{
_asset.buildNumber++;
_asset.CreateNewVersion(_major, _minor, _patch);
_asset.Versions = _asset.Versions.OrderBy(v => v.VersionId).ToList();
}
if (versionInfo != null)
{
EditorGUILayout.BeginVertical(_whiteBoxStyle);
InspectorUtils.InspectorTextWidthRich($"<b>版本{versionInfo.FullVersion}描述: </b>");
versionInfo.Description = EditorGUILayout.TextArea(versionInfo.Description, GUILayout.Height(60));
EditorGUILayout.EndVertical();
}
EditorGUILayout.Space();
if (_asset.Versions.Count == 0) return;
EditorGUILayout.BeginVertical(_redBoxStyle);
InspectorUtils.InspectorTextWidthRich($"<b>版本构建</b>");
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.BeginHorizontal();
if (InspectorUtils.InspectorButtonWithTextWidth($"开启自动战斗")) AddAutoBattle();
if (InspectorUtils.InspectorButtonWithTextWidth($"关闭自动战斗")) RemoveAutoBattle();
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
if (InspectorUtils.InspectorButtonWithTextWidth($"构建测试包"))
{
_asset.CurVersionId = selectedVersion.VersionId;
EditorUtility.SetDirty(_asset);
AssetDatabase.SaveAssets();
// 更新Unity版本号
PlayerSettings.bundleVersion = _asset.FullVersion;
PlayerSettings.bundleVersion = selectedVersion.FullVersion;
// Debug包配置
SetDebugBuildSettings();
// 开始构建
BuildPipeline.BuildPlayer(GetBuildScenes(),
$"{OUTPUT_PATH}/Debug_{_asset.FullVersion}/{Application.productName}.exe",
BuildTarget.StandaloneWindows64,
BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.ConnectWithProfiler);
// BuildPipeline.BuildPlayer(GetBuildScenes(),
// $"../Pack/Debug_{selectedVersion.FullVersion}/{Application.productName}.exe",
// BuildTarget.StandaloneWindows64,
// BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.ConnectWithProfiler);
}
if (GUILayout.Button($"构建 Release 包"))
if (InspectorUtils.InspectorButtonWithTextWidth($"构建发布包"))
{
_asset.buildNumber++;
_asset.CurVersionId = selectedVersion.VersionId;
EditorUtility.SetDirty(_asset);
AssetDatabase.SaveAssets();
// 更新Unity版本号
PlayerSettings.bundleVersion = _asset.FullVersion;
PlayerSettings.bundleVersion = selectedVersion.FullVersion;
// Release包配置
SetReleaseBuildSettings();
// 开始构建
BuildPipeline.BuildPlayer(GetBuildScenes(),
$"{OUTPUT_PATH}/Release_{_asset.FullVersion}/{Application.productName}.exe",
BuildTarget.StandaloneWindows64,
BuildOptions.None);
// // 开始构建
// BuildPipeline.BuildPlayer(GetBuildScenes(),
// $"../Pack/Release_{selectedVersion.FullVersion}/{Application.productName}.exe",
// BuildTarget.StandaloneWindows64,
// BuildOptions.None);
}
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;
// 其他Debug相关设置
PlayerSettings.fullScreenMode = FullScreenMode.Windowed;
PlayerSettings.defaultScreenWidth = 1280;
PlayerSettings.defaultScreenHeight = 720;
PlayerSettings.resizableWindow = true;
// 开启深度剖析器
// PlayerSettings.enableDynamicBatching = 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;
// 其他Release相关设置
PlayerSettings.fullScreenMode = FullScreenMode.FullScreenWindow;
PlayerSettings.defaultScreenWidth = 1920;
PlayerSettings.defaultScreenHeight = 1080;
PlayerSettings.resizableWindow = true;
// 关闭深度剖析器
// PlayerSettings.enableDynamicBatching = false;
PlayerSettings.enableInternalProfiler = false;
// IL2CPP优化
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.Standalone, Il2CppCompilerConfiguration.Release);
PlayerSettings.SetManagedStrippingLevel(BuildTargetGroup.Standalone, ManagedStrippingLevel.High);
// 开启OPS混淆
ObfuscatorSettings.Load().Add_Or_UpdateSettingElement("Global_Enable_Obfuscation", true);
SetObfuscationEnabled(true);
}
private string[] GetBuildScenes()
private void SetObfuscationEnabled(bool enabled)
{
// 获取需要打包的场景列表
var scenes = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
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 AddAutoBattle()
{
string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
if (!defines.Contains(GAME_AUTO_DEBUG))
{
if (scene.enabled)
scenes.Add(scene.path);
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);
}
return scenes.ToArray();
}
}
}

View File

@ -1,47 +0,0 @@
using UnityEditor;
using UnityEngine;
namespace Logic.Editor
{
public class BuildManager
{
// 定义宏名称
public const string GAME_AUTO_DEBUG = "GAME_AUTO_DEBUG";
// 添加宏定义
[MenuItem("Build/开启全自动模式")]
public static void AddMacro()
{
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);
}
}
// 移除宏定义
[MenuItem("Build/关闭全自动模式")]
public static void RemoveMacro()
{
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);
}
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 8640a67a3b834c90bf7ce4a9063fc7c1
timeCreated: 1752049414

View File

@ -6,7 +6,6 @@
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
@ -40,6 +39,7 @@ namespace Logic.Editor
private HashSet<uint> _activeSet = new HashSet<uint>();
private uint _idIndex;
private int _showIndex = 0;
private List<TMP_FontAsset> _assets = new List<TMP_FontAsset>();
[MenuItem("Tools/多语言编辑器")]
@ -53,15 +53,14 @@ namespace Logic.Editor
protected virtual void OnEnable()
{
}
private void OnDisable()
{
}
private void OnGUI()
{
if (!_asset)
@ -77,6 +76,19 @@ namespace Logic.Editor
}
}
if (_assets.Count == 0)
{
var pathList = new List<string>();
pathList.Add($"Assets/Fonts/SourceHanSansCN-Bold SDF.asset");
pathList.Add($"Assets/Fonts/SourceHanSansCN-ExtraLight SDF.asset");
pathList.Add($"Assets/Fonts/SourceHanSansCN-Regular SDF.asset");
foreach (var path in pathList)
{
var asset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(path);
if (asset) _assets.Add(asset);
}
}
if (_redBoxStyle == null)
{
_redBoxStyle = InspectorUtils.GetHelpBoxStyle();
@ -169,11 +181,20 @@ namespace Logic.Editor
EditorGUILayout.BeginHorizontal();
var type = (MultilingualType)(i + 1);
InspectorUtils.InspectorTextWidthRich($"<b>系统语言{type} 对应游戏语言: </b>");
_asset.TargetTypes[i] = (MultilingualType)EditorGUILayout.EnumPopup(_asset.TargetTypes[i]);
_asset.TargetTypes[i] = (MultilingualType)EditorGUILayout.EnumPopup(_asset.TargetTypes[i], GUILayout.Width(200));
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
foreach (var asset in _assets)
{
EditorGUILayout.BeginHorizontal();
if (InspectorUtils.InspectorButtonWithTextWidth($"·")) Selection.activeObject = asset;
EditorGUI.BeginDisabledGroup(true); // 开始禁用组
EditorGUILayout.ObjectField(asset, typeof(TMP_FontAsset), GUILayout.Width(400));
EditorGUI.EndDisabledGroup(); // 结束禁用组
EditorGUILayout.EndHorizontal();
}
var deleteSet = new HashSet<MultilingualFontGroup>();
for (int i = 0; i < _asset.FontGroups.Count; i++)
@ -212,15 +233,15 @@ namespace Logic.Editor
if (InspectorUtils.InspectorButtonWithTextWidth("x")) isDelete = true;
EditorGUILayout.EndHorizontal();
fontGroup.ZHFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ZHFont, typeof(TMP_FontAsset), false);
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ZHFont, typeof(TMP_FontAsset), false, GUILayout.Width(400));
fontGroup.TDZHFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.TDZHFont, typeof(TMP_FontAsset), false);
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.TDZHFont, typeof(TMP_FontAsset), false, GUILayout.Width(400));
fontGroup.ENFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ENFont, typeof(TMP_FontAsset), false);
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ENFont, typeof(TMP_FontAsset), false, GUILayout.Width(400));
fontGroup.JPFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.JPFont, typeof(TMP_FontAsset), false);
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.JPFont, typeof(TMP_FontAsset), false, GUILayout.Width(400));
fontGroup.KRFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.KRFont, typeof(TMP_FontAsset), false);
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.KRFont, typeof(TMP_FontAsset), false, GUILayout.Width(400));
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
return isDelete;