通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1

通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
This commit is contained in:
ALEXTANG
2023-10-23 15:22:34 +08:00
parent 3650ba1a8b
commit d61b1206ee
8 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: defa3b5ccfc64b0392d67db95b3c528d
timeCreated: 1698043573

View File

@@ -0,0 +1,64 @@
using UnityEditor;
using UnityEngine;
using YooAsset.Editor;
namespace TEngine
{
/// <summary>
/// 打包工具类。
/// <remarks>通过CommandLineReader可以不前台开启Unity实现静默打包详见CommandLineReader.cs example1</remarks>
/// </summary>
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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 336706b48f93452ea43f7272872e5451
timeCreated: 1698043593

View File

@@ -0,0 +1,69 @@
using UnityEditor;
using UnityEngine;
/// <summary>
/// Unity编辑器类帮助类。
/// </summary>
public static class ClassHelper
{
/// <summary>
/// 获取MonoBehaviour的脚本Id。
/// </summary>
/// <param name="type">脚本类型。</param>
/// <returns>脚本Id。</returns>
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;
}
/// <summary>
/// 获取MonoBehaviour的脚本Id。
/// </summary>
/// <typeparam name="T">脚本类型。</typeparam>
/// <returns>脚本Id。</returns>
public static int GetClassID<T>() 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<Text>();
if (text != null)
{
var ob = ClassHelper.ReplaceClass(text, typeof(CustomText));
ob.ApplyModifiedProperties();
}
}
}
************************************************************************************************************/
#endregion
/// <summary>
/// 替换MonoBehaviour脚本。
/// </summary>
/// <param name="monoBehaviour"></param>
/// <param name="type"></param>
/// <returns></returns>
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;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa5c786315234462bb549c4533d4d858
timeCreated: 1698042158

View File

@@ -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
/// <summary>
/// Unity命令行拓展帮助类。
/// <remarks>可以用来制定自己项目的打包、编辑器工作流。</remarks>
/// </summary>
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<string,string> GetCustomArguments()
{
Dictionary<string, string> customArgsDict = new Dictionary<string, string>();
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;
}
/// <summary>
/// 获取cmd输入的自定义参数数值。
/// </summary>
/// <param name="argumentName">自定义参数名称。</param>
/// <returns>自定义参数数值。</returns>
public static string GetCustomArgument(string argumentName)
{
Dictionary<string, string> 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 "";
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9f4bc66bca9a460cb714a36a127739e7
timeCreated: 1698041943

View File

@@ -4,6 +4,9 @@ using System.Collections.Generic;
namespace TEngine
{
/// <summary>
/// Unity编辑器主动执行cmd帮助类。
/// </summary>
public static class ShellHelper
{
public static void Run(string cmd, string workDirectory, List<string> environmentVars = null)