Consider the following code from Unity’s plugin sample code:
IEnumerator Start ()
{
CreateTextureAndPassToPlugin();
yield return StartCoroutine("CallPluginAtEndOfFrames");
}
private IEnumerator CallPluginAtEndOfFrames()
{
while (true)
{
// Wait until all frame rendering is done
yield return new WaitForEndOfFrame();
// Set time for the plugin
SetTimeFromUnity (Time.timeSinceLevelLoad);
// Issue a plugin event with arbitrary integer identifier.
// The plugin can distinguish between different
// things it needs to do based on this ID.
// For our simple plugin, it does not matter which ID we pass here.
GL.IssuePluginEvent (1);
}
}
Question:
Why put the “yield return” before “StartCoroutine” in the Start() function?
The function “CallPluginAtEndOfFrames” seems like it will never return. So, it seems like the “yield return” in the Start() function will needlessly prevent the Start() function from returning normally.
From http://docs.unity3d.com/Manual/ExecutionOrder.html :
“[…] yield StartCoroutine Chains the
coroutine, and will wait for the
MyFunc coroutine to complete first.”