Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save unitycoder/0a89f366983e457ab125904b301c3f4c to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/0a89f366983e457ab125904b301c3f4c to your computer and use it in GitHub Desktop.
Unity Coroutine with Callback Return Value and method parameter, or call coroutine from another coroutine without StartCoroutine
IEnumerator Parent()
{
Debug.Log("Parent: before child");
yield return ChildCoroutine();
Debug.Log("Parent: after child");
}
IEnumerator ChildCoroutine()
{
Debug.Log("Child: step 1");
yield return new WaitForSeconds(1f);
Debug.Log("Child: step 2");
}
// caller
StartCoroutine(DoSomething((success) =>
{
});
int myarg=1;
StartCoroutine(DoSomething(myarg, (Bool success) =>
{
if (success)
{
Debug.Log("Success");
}
else
{
Debug.Log("Failed");
}
}
}));
// coroutine
IEnumerator DoSomething(int arg, Action<bool> callback)
{
if (arg==0)
{
callback(false);
}
callback(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment