mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
通过CommandLineReader可以不前台开启Unity实现静默打包,详见CommandLineReader.cs example1
This commit is contained in:
69
UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs
Normal file
69
UnityProject/Assets/TEngine/Editor/Utility/ClassHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa5c786315234462bb549c4533d4d858
|
||||
timeCreated: 1698042158
|
120
UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs
Normal file
120
UnityProject/Assets/TEngine/Editor/Utility/CommandLineReader.cs
Normal 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 "";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f4bc66bca9a460cb714a36a127739e7
|
||||
timeCreated: 1698041943
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user