From d61b1206eeaeec31aff671e58eea76020f81f1b2 Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Mon, 23 Oct 2023 15:22:34 +0800
Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87CommandLineReader=E5=8F=AF?=
=?UTF-8?q?=E4=BB=A5=E4=B8=8D=E5=89=8D=E5=8F=B0=E5=BC=80=E5=90=AFUnity?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=9D=99=E9=BB=98=E6=89=93=E5=8C=85=EF=BC=8C?=
=?UTF-8?q?=E8=AF=A6=E8=A7=81CommandLineReader.cs=20example1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
---
.../Assets/TEngine/Editor/ReleaseTools.meta | 3 +
.../Editor/ReleaseTools/ReleaseTools.cs | 64 ++++++++++
.../Editor/ReleaseTools/ReleaseTools.cs.meta | 3 +
.../TEngine/Editor/Utility/ClassHelper.cs | 69 ++++++++++
.../Editor/Utility/ClassHelper.cs.meta | 3 +
.../Editor/Utility/CommandLineReader.cs | 120 ++++++++++++++++++
.../Editor/Utility/CommandLineReader.cs.meta | 3 +
.../TEngine/Editor/Utility/ShellHelper.cs | 3 +
8 files changed, 268 insertions(+)
create mode 100644 UnityProject/Assets/TEngine/Editor/ReleaseTools.meta
create mode 100644 UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs
create mode 100644 UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs.meta
create mode 100644 UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs
create mode 100644 UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs.meta
create mode 100644 UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs
create mode 100644 UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs.meta
diff --git a/UnityProject/Assets/TEngine/Editor/ReleaseTools.meta b/UnityProject/Assets/TEngine/Editor/ReleaseTools.meta
new file mode 100644
index 000000000..af5b42977
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/ReleaseTools.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: defa3b5ccfc64b0392d67db95b3c528d
+timeCreated: 1698043573
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs b/UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs
new file mode 100644
index 000000000..15fb81fb4
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs
@@ -0,0 +1,64 @@
+using UnityEditor;
+using UnityEngine;
+using YooAsset.Editor;
+
+namespace TEngine
+{
+ ///
+ /// 打包工具类。
+ /// 通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
+ ///
+ public static class ReleaseTools
+ {
+ public static void BuildPackage()
+ {
+ string outputRoot = CommandLineReader.GetCustomArgument("outputRoot");
+ BuildTarget target = BuildTarget.StandaloneWindows64;
+ BuildInternal(target,outputRoot);
+ Debug.LogWarning($"Start BuildPackage BuildTarget:{target} outputPath:{outputRoot}");
+ }
+
+ private static void BuildInternal(BuildTarget buildTarget,string outputRoot)
+ {
+ Debug.Log($"开始构建 : {buildTarget}");
+
+ // 构建参数
+ BuildParameters buildParameters = new BuildParameters();
+ buildParameters.StreamingAssetsRoot = AssetBundleBuilderHelper.GetDefaultStreamingAssetsRoot();
+ buildParameters.BuildOutputRoot = outputRoot;//AssetBundleBuilderHelper.GetDefaultBuildOutputRoot();
+ buildParameters.BuildTarget = buildTarget;
+ buildParameters.BuildPipeline = EBuildPipeline.BuiltinBuildPipeline;
+ buildParameters.BuildMode = EBuildMode.ForceRebuild;
+ buildParameters.PackageName = "DefaultPackage";
+ buildParameters.PackageVersion = "1.0";
+ buildParameters.VerifyBuildingResult = true;
+ buildParameters.SharedPackRule = new ZeroRedundancySharedPackRule();
+ buildParameters.CompressOption = ECompressOption.LZ4;
+ buildParameters.OutputNameStyle = EOutputNameStyle.HashName;
+ buildParameters.CopyBuildinFileOption = ECopyBuildinFileOption.None;
+
+ // 执行构建
+ AssetBundleBuilder builder = new AssetBundleBuilder();
+ var buildResult = builder.Run(buildParameters);
+ if (buildResult.Success)
+ {
+ Debug.Log($"构建成功 : {buildResult.OutputPackageDirectory}");
+ }
+ else
+ {
+ Debug.LogError($"构建失败 : {buildResult.ErrorInfo}");
+ }
+ }
+
+ // 从构建命令里获取参数示例
+ private static string GetBuildPackageName()
+ {
+ foreach (string arg in System.Environment.GetCommandLineArgs())
+ {
+ if (arg.StartsWith("buildPackage"))
+ return arg.Split("="[0])[1];
+ }
+ return string.Empty;
+ }
+ }
+}
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs.meta b/UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs.meta
new file mode 100644
index 000000000..6c8f8f30e
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 336706b48f93452ea43f7272872e5451
+timeCreated: 1698043593
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs b/UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs
new file mode 100644
index 000000000..531f860d6
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs
@@ -0,0 +1,69 @@
+using UnityEditor;
+using UnityEngine;
+
+///
+/// Unity编辑器类帮助类。
+///
+public static class ClassHelper
+{
+ ///
+ /// 获取MonoBehaviour的脚本Id。
+ ///
+ /// 脚本类型。
+ /// 脚本Id。
+ public static int GetClassID(System.Type type)
+ {
+ GameObject gameObject = EditorUtility.CreateGameObjectWithHideFlags("Temp", HideFlags.HideAndDontSave);
+ Component component = gameObject.AddComponent(type);
+ SerializedObject @class = new SerializedObject(component);
+ int classID = @class.FindProperty("m_Script").objectReferenceInstanceIDValue;
+ Object.DestroyImmediate(gameObject);
+ return classID;
+ }
+
+ ///
+ /// 获取MonoBehaviour的脚本Id。
+ ///
+ /// 脚本类型。
+ /// 脚本Id。
+ public static int GetClassID() where T : MonoBehaviour
+ {
+ return GetClassID(typeof(T));
+ }
+
+ #region Method Documentation
+ /************************************************************************************************************
+ Example:
+ [MenuItem("GameObject/UI/转化成CustomText", false, 1999)]
+ public static void ConvertToCustomText(MenuCommand menuCommand)
+ {
+ GameObject go = menuCommand.context as GameObject;
+ if (go != null)
+ {
+ Text text = go.GetComponent();
+ if (text != null)
+ {
+ var ob = ClassHelper.ReplaceClass(text, typeof(CustomText));
+ ob.ApplyModifiedProperties();
+ }
+ }
+ }
+ ************************************************************************************************************/
+ #endregion
+ ///
+ /// 替换MonoBehaviour脚本。
+ ///
+ ///
+ ///
+ ///
+ public static SerializedObject ReplaceClass(MonoBehaviour monoBehaviour, System.Type type)
+ {
+ int classID = GetClassID(type);
+ SerializedObject @class = new SerializedObject(monoBehaviour);
+ @class.Update();
+ @class.FindProperty("m_Script").objectReferenceInstanceIDValue = classID;
+ @class.ApplyModifiedProperties();
+ @class.Update();
+ return @class;
+ }
+}
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs.meta b/UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs.meta
new file mode 100644
index 000000000..8150e8c76
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: aa5c786315234462bb549c4533d4d858
+timeCreated: 1698042158
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs b/UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs
new file mode 100644
index 000000000..5167c4d5c
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs
@@ -0,0 +1,120 @@
+#region Class Documentation
+/************************************************************************************************************
+Class Name: CommandLineReader.cs
+Type: Util, Static
+Definition:
+ CommandLineReader.cs give the ability to access [Custom Arguments] sent
+ through the command line. Simply add your custom arguments under the
+ keyword '-CustomArgs:' and seperate them by ';'.
+Example:
+ C:\Program Files (x86)\Unity\Editor\Unity.exe [ProjectLocation] -executeMethod [Your entrypoint] -quit -CustomArgs:Language=en_US;Version=1.02
+
+Example1:
+ set WORKSPACE=.
+ set UNITYEDITOR_PATH=G:/UnityEditor/2021.3.20f1c1/Editor
+ set LOGFILE=./build.log
+ set BUILDROOT=G:/github/TEngine/UnityProject/Bundles
+
+ %UNITYEDITOR_PATH%/Unity.exe %WORKSPACE%/UnityProject -logFile %LOGFILE% -executeMethod TEngine.ReleaseTools.BuildPackage -quit -batchmode -CustomArgs:Language=en_US;Version=1.02;outputRoot=%BUILDROOT%
+
+ @REM for /f "delims=[" %%i in (%LOGFILE%) do echo %%i
+
+ pause
+************************************************************************************************************/
+#endregion
+
+#region Using
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using UnityEditor;
+using Debug = UnityEngine.Debug;
+#endregion
+
+///
+/// Unity命令行拓展帮助类。
+/// 可以用来制定自己项目的打包、编辑器工作流。
+///
+public class CommandLineReader
+{
+ //Config
+ private const string CUSTOM_ARGS_PREFIX = "-CustomArgs:";
+ private const char CUSTOM_ARGS_SEPARATOR = ';';
+
+ public static string[] GetCommandLineArgs()
+ {
+ return Environment.GetCommandLineArgs();
+ }
+
+ public static string GetCommandLine()
+ {
+ string[] args = GetCommandLineArgs();
+
+ if (args.Length > 0)
+ {
+ return string.Join(" ", args);
+ }
+ else
+ {
+ Debug.LogError("CommandLineReader.cs - GetCommandLine() - Can't find any command line arguments!");
+ return "";
+ }
+ }
+
+ public static Dictionary GetCustomArguments()
+ {
+ Dictionary customArgsDict = new Dictionary();
+ string[] commandLineArgs = GetCommandLineArgs();
+ string[] customArgs;
+ string[] customArgBuffer;
+ string customArgsStr = "";
+
+ try
+ {
+ customArgsStr = commandLineArgs.Where(row => row.Contains(CUSTOM_ARGS_PREFIX)).Single();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError("CommandLineReader.cs - GetCustomArguments() - Can't retrieve any custom arguments in the command line [" + commandLineArgs + "]. Exception: " + e);
+ return customArgsDict;
+ }
+
+ customArgsStr = customArgsStr.Replace(CUSTOM_ARGS_PREFIX, "");
+ customArgs = customArgsStr.Split(CUSTOM_ARGS_SEPARATOR);
+
+ foreach (string customArg in customArgs)
+ {
+ customArgBuffer = customArg.Split('=');
+ if (customArgBuffer.Length == 2)
+ {
+ customArgsDict.Add(customArgBuffer[0], customArgBuffer[1]);
+ }
+ else
+ {
+ Debug.LogWarning("CommandLineReader.cs - GetCustomArguments() - The custom argument [" + customArg + "] seem to be malformed.");
+ }
+ }
+
+ return customArgsDict;
+ }
+
+ ///
+ /// 获取cmd输入的自定义参数数值。
+ ///
+ /// 自定义参数名称。
+ /// 自定义参数数值。
+ public static string GetCustomArgument(string argumentName)
+ {
+ Dictionary customArgsDict = GetCustomArguments();
+
+ if (customArgsDict.TryGetValue(argumentName, out var argument))
+ {
+ return argument;
+ }
+ else
+ {
+ Debug.LogError("CommandLineReader.cs - GetCustomArgument() - Can't retrieve any custom argument named [" + argumentName + "] in the command line [" + GetCommandLine() + "].");
+ return "";
+ }
+ }
+}
diff --git a/UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs.meta b/UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs.meta
new file mode 100644
index 000000000..66cfde6af
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 9f4bc66bca9a460cb714a36a127739e7
+timeCreated: 1698041943
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Editor/Utility/ShellHelper.cs b/UnityProject/Assets/TEngine/Editor/Utility/ShellHelper.cs
index 6f6eb68d7..057a47519 100644
--- a/UnityProject/Assets/TEngine/Editor/Utility/ShellHelper.cs
+++ b/UnityProject/Assets/TEngine/Editor/Utility/ShellHelper.cs
@@ -4,6 +4,9 @@
namespace TEngine
{
+ ///
+ /// Unity编辑器主动执行cmd帮助类。
+ ///
public static class ShellHelper
{
public static void Run(string cmd, string workDirectory, List environmentVars = null)