Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to start Unity in batchmode in background using PHP?

Discussion in 'Asset Bundles' started by thesanketkale, Apr 16, 2021.

  1. thesanketkale

    thesanketkale

    Joined:
    Dec 14, 2016
    Posts:
    63
    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 or not it started?

    P.S. - I tried pclose(popen($commandStr, 'r')); but that doesn't start the unity instance at all. Also, I was not sure which forum to ask this on, but here I am. Please help!
     
  2. thesanketkale

    thesanketkale

    Joined:
    Dec 14, 2016
    Posts:
    63
    I figured it out. 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!'
    }

    ?>


    Putting it out here in case somebody needs it.