Skip to content

Commit

Permalink
通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
Browse files Browse the repository at this point in the history
通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
  • Loading branch information
ALEXTANGXIAO committed Oct 23, 2023
1 parent 3650ba1 commit d61b120
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 0 deletions.
3 changes: 3 additions & 0 deletions UnityProject/Assets/TEngine/Editor/ReleaseTools.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions UnityProject/Assets/TEngine/Editor/ReleaseTools/ReleaseTools.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions UnityProject/Assets/TEngine/Editor/Utility/ShellHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace TEngine
{
/// <summary>
/// Unity编辑器主动执行cmd帮助类。
/// </summary>
public static class ShellHelper
{
public static void Run(string cmd, string workDirectory, List<string> environmentVars = null)
Expand Down

0 comments on commit d61b120

Please sign in to comment.