Unity command line — how to acces existing instance without -quit(ing) for every command

I’m successfully able to run basic Unity Editor functions from the command line, with a command like this:

"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"

and some C# code like this:

 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);
        }
    }

which is actually working great so far (as long as I put the C# file in the “Assets/Editor” folder), both when I run it as a MenuItem in the Unity Editor and even when I call the command from the command line.

The problem:

I would like to be able to call multiple commands from the Unity Editor (after checking that the log file has completed one command, for example). Currently, to do this, I need to add the -quit argument for each command line call, which quits the entire unity instance, and then when I want to run a new command, it has to start up unity all over again, which could take a lot of time if I was running many commands.

How can I simply access the already-running unity instance that’s either running in batchmode, or even if its open in the editor? Meaning, when I take out the -quit parameter, then the command still works, but it’s clear that Unity is still running (as I can see from the Task Manager and also when I try to run the command again I get an error in the log that Unity is already running the current project), and obviously when the actual Unity Editor is running with the project open, then the log gives the error that another Unity instance is running with this project etc…

So is there a way to simply get the current Unity instance that’s running with the desired project, and just run a command from that, or do I have to restart Unity every time I want to run a command from the command line?

1 Like

I don’t have any answer to your specific question, but why even quit your instance in the first place if you have more work to do? I’d make sure to do all the work before exiting.

There’s no builtin way to access existing instance. You can open a file and wait for commands to appear there, then parse them by your script and execute.

Do you find a way to solve it?

I found one of your post on stack overflow: Unity command line -- how to acces existing instance without -quit(ing) for every command - Stack Overflow

You mentioned that you used a websocket to send commands with nodejs. Do you have more details on that?