1

I am trying to start Unity instance in batch mode with -executeMethod to build asset bundles of more than 2000 textures using a command like this:

"C:\Program Files\Unity\Hub\Editor\2020.3.1f1\Editor\Unity.exe" -batchmode -projectPath "D:\UnityWorkbench\2020\Build Texture Asset Bundle" -executeMethod DummyNamespace.TexturesManager.BuildTextureAssetBundles -env TEST

Now, I want to create a REST API using PHP to execute this command on a AWS EC2 windows server. I used the below script to start the instance and execute the static method in the unity project.

<?php

$commandStr = '"C:\Program Files\Unity\Hub\Editor\2020.3.1f1\Editor\Unity.exe" -batchmode -projectPath "D:\UnityWorkbench\2020\Build Texture Asset Bundle" -executeMethod DummyNamespace.TexturesManager.BuildTextureAssetBundles -env TEST';
$output=null;
$retval=null;
exec($commandStr, $output, $retval);
echo "retval: ".$retval."\n\n";
echo "output: ".$output."\n\n";

?>

This script starts the unity instance and then waits for its execution to return the response. Hence, it always reaches PHP request timeout as it builds more than 2000 images in the asset bundle, which could take some time to complete.

How do I make it start the instance in batch mode in the background and immediately return a response of whether it started or not?

P.S. - I tried pclose(popen($commandStr, 'r')); but that doesn't start the unity instance at all. Please help!

Sanket Kale
  • 33
  • 1
  • 11
  • Make your timeout longer? You can't immediately return something that doesn't exist immediately ... Sounds like a RestAPI is maybe not the way to go ... Maybe rather a TCP connection or something like that – derHugo Apr 16 '21 at 20:03
  • Making timeout longer would be counter-intuitive in this case as I am planning to put a button on a backend website that will make the REST API call on click. I just want it to start a background process/thread for this where once started, I can return the status of whether or not it started and then check progress in the logs. Is there any other way I can implement this? – Sanket Kale Apr 17 '21 at 06:29

1 Answers1

0

After searching and trying different methods for a couple of days, I finally got it working by doing what it was said in a similar SO question here. It worked in my case where the command was going to start Unity instance in batch mode as well.

The code looks like this:

<?php

$commandStr = '"C:\Program Files\Unity\Hub\Editor\2020.3.1f1\Editor\Unity.exe" -batchmode -projectPath "D:\UnityWorkbench\2020\Build Texture Asset Bundle" -executeMethod DummyNamespace.TexturesManager.BuildTextureAssetBundles -env TEST';

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($commandStr, 0, false);

if($oExec==0){
    echo 'Unity started in background!'
} else {
    echo 'Failure starting Unity in background!'
}

?>
Sanket Kale
  • 33
  • 1
  • 11