我可以使用以下命令从命令行成功运行Unity编辑器的基本功能:
"C:\Program Files\Unity\Hub\Editor\2019.1.0b1\Editor\Unity" -batchmode -quit -projectPath "C:\Users\me\OneDrive\Documents\totally empty" -executeMethod COBY.BuildIt -logfile "C:\Users\me\OneDrive\Desktop\wow.txt"
和一些这样的C#代码:
public class COBY : MonoBehaviour
{
public static void BuildIt() {
AssetBundleBuild[] assets = new AssetBundleBuild[2];
assets[0].assetBundleName = "cobysNewBundle";
string[] assetNames = new string[2];
assetNames[0] = "Assets/CobyThings/teffilin.fbx";
assetNames[1] = "Assets/CobyThings/up.fbx";
assets[0].assetNames = assetNames;
assets[1].assetBundleName = "anotherBundle";
string[] notherBundleNames = new string[1];
notherBundleNames[0] = "Assets/CobyThings/atzmusPic.png";
assets[1].assetNames = notherBundleNames;
var path = "Assets/m23214ycobytest.txt";
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine("Just wrote to these assets!");
writer.Close();
Debug.Log("ASDFGG");
MonoBehaviour.print("wow");
BuildPipeline.BuildAssetBundles("Assets/CobysAssets", assets, BuildAssetBundleOptions.None, BuildTarget.WebGL);
}
}
到目前为止(实际上,只要我将C#文件放在“ Assets / Editor”文件夹中即可),无论是当我在Unity Editor中将其作为MenuItem运行时,甚至是从命令行。
问题:
我希望能够从Unity编辑器中调用多个命令(例如,在检查日志文件已完成一个命令之后)。当前,要执行此操作,我需要为每个命令行调用添加-quit参数,这将退出整个unity实例,然后,当我要运行新命令时,它必须重新启动unity,这可以如果我正在运行许多命令,则会花费很多时间。
如何简单地访问以批处理方式运行或已经在编辑器中打开的已经运行的统一实例?意思是,当我取出-quit参数时,该命令仍然有效,但很明显Unity仍在运行(如从任务管理器中看到的,以及当我再次尝试运行该命令时,在记录Unity已经在运行当前项目),显然当实际Unity Editor在打开项目的情况下运行时,该日志给出了另一个Unity实例正在与此项目运行的错误信息,等等...
因此,有一种方法可以简单地获取正在与所需项目一起运行的当前Unity实例,并从中运行命令,或者我必须在每次我想运行时重新启动Unity来自命令行的命令?
答案 0 :(得分:0)
可能不完全是您想要的-afaik无法将命令行参数发送到已经运行的进程-但是您可以使用一个集中的-executeMethod
调用和System.Environment.GetCommandLineArgs();
来获取一些自定义参数为了告诉中心方法该怎么做...类似
public class COBY : MonoBehaviour
{
public static void ExecuteFromExternal()
{
string[] args = System.Environment.GetCommandLineArgs ();
// maybe also add a flag to make sure a second thing is only done
// if the one before was successfull
bool lastSucceeded = true;
// just in combination with the flag to know which was the last
// command executed
int lastI = 0;
for (int i = 0; i < args.Length; i++)
{
Debug.Log ("ARG " + i + ": " + args [i]);
// interrupt if one command failed
if(!lastSucceeded)
{
Debug.LogFormat("Last command \"{0}\" failed! -> cancel", args[lastI]);
return;
}
switch(args[i])
{
case "-doSomething":
lastI = i;
// do something
lastSucceeded = wasItSuccessfull;
break;
case "-build":
lastI = i;
// maybe you want to get some values after "-build"
// because they are needed let's say e.g. an int and a path
int value = args[i + 1];
string path = args[i + 2];
// maybe a sanity check here
// now build using that value and the path
// you could increase i since the next two
// arguments don't have to be checked
i += 2;
break;
}
}
}
}
因此您现在可以致电例如
"C:\...\Unity" -batchmode -quit -projectPath "C:\..." -executeMethod COBY.ExecuteFromExternal -doSomething -logfile "C:\...\wow.txt"
仅执行第一种方法或
"C:\...\Unity" -batchmode -quit -projectPath "C:\..." -executeMethod COBY.ExecuteFromExternal -build 42 "C:\some\path" -logfile "C:\...\wow.txt"
仅执行构建部分或
"C:\...\Unity" -batchmode -quit -projectPath "C:\..." -executeMethod COBY.ExecuteFromExternal -doSomething -build 42 "C:\some\path" -logfile "C:\...\wow.txt"
(按顺序执行)。
只要您没有做任何async
的工作,就应该没问题,并且第二个方法不应在第一个方法完成之前执行。使用标志lastSucceeded
,您还可以控制是否要进行第二件事,如果之前的失败了。