Use Coroutine in Batchmode

I am writing a build script that is lunched from the command line with -quit -batchmode -executeMethod and I want to use a UnityWebRequest to obtain stuff from my server. Is there a way to launch a coroutine and wait for it to finish in such a static method script or will I need to rely on threads and the god awful HttpWebRequest of .net 3.5?

I doubt that Update, and with that coroutines, work in this context. But it should work to just wait for a WWW or UnityWebRequest to finish. Afaik, they are not bound to be stuffed into a coroutine in any way.

Here’s some example code (with WWW, but the idea is the same) you could try:

public static void Foo()
{
  var www = new WWW(url);
  while(!www.isDone)
  {
    Thread.Sleep(100);
  }
  DoStuffWith(www);
}

To make it work, you need to remove the -quit flag from your command line invocation and instead call EditorApplication.Exit when the build is finished. That way, Unity will not exit when the static method you invoked has finished executing, and you can use your static method to start a coroutine instead. Like so:

private class EditorCoroutine
{
	private IEnumerator routine;

	private EditorCoroutine(IEnumerator routine)
	{
		this.routine = routine;
	}

	public static EditorCoroutine Start(IEnumerator routine)
	{
		EditorCoroutine coroutine = new EditorCoroutine(routine);
		coroutine.Start();
		return coroutine;
	}

	private void Start()
	{
		UnityEditor.EditorApplication.update += Update;
	}

	public void Stop()
	{
		UnityEditor.EditorApplication.update -= Update;
	}

	private void Update()
	{
		if (!routine.MoveNext())
		{
			Stop();
		}
	}
}

public static class AutomatedBuilder
{
    // Invoke that one from the command line
	public static void Build()
	{
		EditorCoroutine.Start(BuildImpl());
	}

	private static System.Collections.IEnumerator BuildImpl()
	{
		// Your build code here...
		yield return null;
		EditorApplication.Exit(0);
	}
}

A soultion to this if you cannot remove the -quit option (like if you are running automated builds with TeamCity) is to run a coroutine “manually” like this:

public static void BuildMethod() {
    var it = CoroutineMethod();
    while (it.MoveNext()) { }
    //done here and unity will exit
}