/dev/null 2>&1&"; system($cmd, $res); } }); task('unity_kill', function() { $proc_id = unity_find_proc_id(); if($proc_id) { echo "Found Unity process '$proc_id', killing it...\n"; system("kill -9 $proc_id"); sleep(5); } else { echo "No Unity process found to kill\n"; } }); function unity_exec($func, $build_target = "", $quit = true, $batchmode = true) { global $GAME_ROOT; $proj_path = normalize_path("$GAME_ROOT/"); $log_file = "$GAME_ROOT/build/unity.log"; ensure_rm($log_file); $pid_file = "$GAME_ROOT/build/unity.pid"; $username = getor('UNITY_ACCOUNT_USERNAME', null); $password = getor('UNITY_ACCOUNT_PASSWORD', null); $shared_cmd = "-projectPath $proj_path -logFile $log_file " . ($batchmode ? "-batchmode" : "") . " -accept-apiupdate" . ($quit ? " -quit" : "") . ($build_target ? " -buildTarget $build_target" : "") . ($func != "" ? " -executeMethod $func" : ""); if($username !== null && $password !== null) $shared_cmd .= " -username \"$username\" -password \"$password\""; $pid = unity_run_proc($shared_cmd); if(!$pid) throw new Exception("Error starting cmd: $shared_cmd"); while(!is_file($log_file)) usleep(200000); return array($log_file, $pid); } function unity_batch_exec($func, $build_target = "", $notify = true) { global $GAME_ROOT; try { list($log_file, $pid) = unity_exec($func, $build_target, /*$quit = */ true, /*$batchmode = */ true); watch_running_process($pid, $log_file, array( 'Exiting batchmode successfully' ), array( 'UnityException:', 'Aborting batchmode due to failure', 'Launching bug reporter', ': error CS', 'Unrecognized assets cannot be included in AssetBundles:' ) ); } catch(Exception $e) { throw $e; } } function unity_run_proc($shared_cmd) { $app_dir = get('UNITY_APP_DIR'); echo "Unity App dir: $app_dir\n"; if(is_win()) { $unity_app_path = $app_dir . "Editor/Unity.exe"; $cmd = "powershell.exe (Start-Process '$unity_app_path'-ArgumentList '$shared_cmd' -passthru).Id"; } else { $cmd = "'$app_dir/Unity.app/Contents/MacOS/Unity' $shared_cmd > /dev/null & echo $!"; } exec($cmd, $out, $ret); if($ret !== 0) throw new Exception("Error starting Unity: $cmd ($ret)"); return trim($out[0]); } function unity_find_proc_id() { global $GAME_ROOT; exec("ps aux | grep 'Unity' | grep -i 'projectpath' | grep -v grep", $out); foreach($out as $line) if(preg_match('~.*?\s+(\d+).*-projectpath\s+(.*?)\s+-~i', $line, $ms)) if(strpos(realpath($ms[2]), realpath($GAME_ROOT)) === 0) return $ms[1]; return null; } function guess_unity_app_dir() { global $GAME_ROOT; if(getenv("UNITY_APP_DIR")) return getenv("UNITY_APP_DIR"); $path = "/Applications/Unity/"; if(is_win()) $path = getenv("ProgramFiles")."/Unity/"; $proj_version_file = $GAME_ROOT.'/unity/ProjectSettings/ProjectVersion.txt'; if(is_file($proj_version_file)) { list($_, $unity_version) = array_map('trim', explode(":", file($proj_version_file)[0])); if(is_dir("{$path}Hub/Editor/$unity_version/")) $path .= "Hub/Editor/$unity_version/"; } return $path; } function get_unity_dir() { $app_dir = get('UNITY_APP_DIR'); if(is_win()) return "$app_dir/Editor/Data"; else return "$app_dir/Unity.app/Contents/"; }