public static void Launch(LaunchOptions options) { Logger.Verbose = options.Verbose; var unityProject = new UnityProject(options.ProjectPath); Logger.Info("Attempting to launch Unity with project '{0}'", options.ProjectPath); if (!unityProject.IsValid()) { Logger.Error("The passed project doesn't seem like a valid Unity project."); Environment.ExitCode = 1; return; } var unityHub = new UnityHub(options.HubPath); if (!unityHub.PathExists()) { Logger.Error("Unity hub was not found"); Environment.ExitCode = 1; return; } var unityInstall = unityHub.GetInstall(unityProject.UnityVersion); if (unityInstall == null) { if (!options.InstallIfNeeded) { Logger.Error("Unity version '{0}' doesn't seem to be installed", unityProject.UnityVersion); Logger.Info("You can use the '--install-if-needed' option to automatically install it"); Environment.ExitCode = 1; return; } Logger.Info("Unity version is not yet installed, doing that next"); if (!InstallVersion(unityHub, unityProject.UnityVersion, unityProject.UnityVersionChangeset)) { Logger.Error("Failed to install, not opening project"); Environment.ExitCode = 1; return; } unityInstall = unityHub.GetInstall(unityProject.UnityVersion); } var unityArgs = new UnityLaunchArguments(unityProject); options.CopyTo(unityArgs); if (options.Headless) { unityArgs.SetHeadless(); } var exitCode = unityInstall.Launch(unityArgs); Environment.ExitCode = exitCode; }
/// <summary> /// Launch this Unity install /// </summary> /// <param name="arguments">Arguments to launch Unity with</param> /// <returns>Exit code from unity if <see cref="UnityLaunchArguments.WaitForExit"/> is true, otherwise 0</returns> public int Launch(UnityLaunchArguments arguments) { var argumentsString = arguments.ToString(); Logger.Info("Launching Unity '{0}' with the following arguments: '{1}'", Version, arguments); using var process = new Process(); process.StartInfo.FileName = ExecutablePath; process.StartInfo.Arguments = argumentsString; process.StartInfo.WindowStyle = arguments.BatchMode ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal; if (arguments.WaitForExit) { process.StartInfo.CreateNoWindow = false; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data); process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data); } else { process.StartInfo.UseShellExecute = true; } process.Start(); if (arguments.WaitForExit) { Logger.Info("Unity Output:"); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); Logger.Debug("Unity has finished with exit code {0}", process.ExitCode); return(process.ExitCode); } return(0); }