Unity命令行 - 如何为每个命令访问不带-quit(ing)的现有实例

问题描述 投票:0回答:1

我已经成功地从命令行运行基本的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编辑器中将它作为MenuItem运行时,甚至当我从命令行调用命令时。

问题:

我希望能够从Unity Editor调用多个命令(例如,在检查日志文件已完成一个命令之后)。目前,要做到这一点,我需要为每个命令行调用添加-quit参数,它退出整个统一实例,然后当我想运行一个新命令时,它必须重新启动统一,这可能如果我运行很多命令,需要花很多时间。

我怎样才能简单地访问已在批处理模式下运行的已经运行的统一实例,或者即使它在编辑器中打开?这意味着,当我取出-quit参数时,该命令仍然有效,但很明显Unity仍在运行(正如我从任务管理器中看到的那样,当我再次尝试运行命令时,我得到一个错误记录Unity已在运行当前项目),显然当实际Unity Editor在项目打开的情况下运行时,日志会显示另一个Unity实例正在运行此项目的错误等...

那么有没有办法简单地获取当前正在运行所需项目的Unity实例,并从中运行命令,或者每次我想从命令行运行命令时是否必须重新启动Unity?

unity3d command-line
1个回答
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你也可以控制是否应该做第二件事,如果之前失败或不失败。

© www.soinside.com 2019 - 2024. All rights reserved.