Start-Process with Unity in Batch Mode Hangs and Never Exits in PowerShell Script

113 views Asked by At

I am using PowerShell to automate the build process of a Unity project. I am running Unity in batch mode using Start-Process, but I've encountered a problem where the process hangs indefinitely after completing the build, and never reaches the line where it gets the exit code. Below is the relevant part of my script:

$UnityPath = "C:\Program Files\Unity\Hub\Editor\2023.1.17f1\Editor\Unity.exe"
$BuildMethod = "Assets.Scripts.Editor.BuildScript.BuildProject"

Write-Host "Building project..."
$process = Start-Process -FilePath $UnityPath -ArgumentList "-batchmode", "-executeMethod", $BuildMethod -NoNewWindow -PassThru -Wait
$exitCode = $process.ExitCode
Write-Host "Unity process exited with code: $exitCode"

After running this command, I get the following output indicating the build is successful:

Build succeeded: 44940173 bytes
UnityEngine.StackTraceUtility:ExtractStackTrace ()
UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
UnityEngine.Logger:Log (UnityEngine.LogType,object)
UnityEngine.Debug:Log (object)
Assets.Scripts.Editor.BuildScript:BuildProject () (at Assets/Scripts/Editor/BuildScript.cs:31)

However, the process never exits, and it doesn't proceed to the line $exitCode = $process.ExitCode, effectively hanging the script.

I've checked my Unity build script (BuildProject) for any issues but couldn't find any apparent causes for this behavior. The build completes successfully when run manually in Unity, and no user input is expected or required in the script.

I'm looking for insights on why Start-Process hangs and how I can ensure it exits properly after the build is complete. Is there a known issue with running Unity in batch mode this way, or could there be something in my build script that's causing this behavior?

Any advice or suggestions to debug and resolve this issue would be greatly appreciated.

0

There are 0 answers