本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_Coroutine.html 
using UnityEngine;
using System.Collections; public class CoroutineTest : MonoBehaviour { void Start () {
print("Starting " + Time.time);
StartCoroutine(WaitAndPrint(0.2F));
print("Before WaitAndPrint Finishes " + Time.time);
} IEnumerator WaitAndPrint(float waitTime) {
print("StartCoroutine1 " + Time.time);
StartCoroutine("DoSomething", 2.0f);
print("StartCoroutine2 " + Time.time);
yield return new WaitForSeconds(waitTime);
print("StopCoroutine1 " + Time.time);
StopCoroutine("DoSomething");
print("StopCoroutine2 " + Time.time);
} IEnumerator DoSomething(float someParameter) {
while (true) {
print("DoSomething Loop");
yield return null;
}
} void Projectile() {
print("Projectile " + Time.time);
} void LaunchProjectile() {
print("LaunchProjectile " + Time.time);
} // Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space) && !IsInvoking("LaunchProjectile")) {
InvokeRepeating("LaunchProjectile", 2f, 0.5f);
Invoke("Projectile", 2f);
}
if (Input.GetKeyDown(KeyCode.X)) {
Debug.Log("CancelInvoke");
CancelInvoke("LaunchProjectile");
}
}
}
#region Assembly UnityEngine.dll, v0.0.0.0
// D:\Program\Unity Project\test\Library\UnityAssemblies\UnityEngine.dll
#endregion using System;
using System.Collections;
using UnityEngine.Internal;
using UnityEngine.Scripting; namespace UnityEngine {
// Summary:
// MonoBehaviour is the base class every script derives from.
[RequiredByNativeCode]
public class MonoBehaviour : Behaviour {
[WrapperlessIcall]
public MonoBehaviour(); // Summary:
// Disabling this lets you skip the GUI layout phase.
public bool useGUILayout { get; set; } // Summary:
// Cancels all Invoke calls on this MonoBehaviour.
public void CancelInvoke();
//
// Summary:
// Cancels all Invoke calls with name methodName on this behaviour.
//
// Parameters:
// methodName:
[WrapperlessIcall]
public void CancelInvoke(string methodName);
//
// Summary:
// Invokes the method methodName in time seconds.
//
// Parameters:
// methodName:
//
// time:
[WrapperlessIcall]
public void Invoke(string methodName, float time);
//
// Summary:
// Invokes the method methodName in time seconds, then repeatedly every repeatRate
// seconds.
//
// Parameters:
// methodName:
//
// time:
//
// repeatRate:
[WrapperlessIcall]
public void InvokeRepeating(string methodName, float time, float repeatRate);
//
// Summary:
// Is any invoke pending on this MonoBehaviour?
public bool IsInvoking();
//
// Summary:
// Is any invoke on methodName pending?
//
// Parameters:
// methodName:
[WrapperlessIcall]
public bool IsInvoking(string methodName);
//
// Summary:
// Logs message to the Unity Console (identical to Debug.Log).
//
// Parameters:
// message:
public static void print(object message);
//
// Summary:
// Starts a coroutine.
//
// Parameters:
// routine:
public Coroutine StartCoroutine(IEnumerator routine);
//
// Summary:
// Starts a coroutine named methodName.
//
// Parameters:
// methodName:
//
// value:
[ExcludeFromDocs]
public Coroutine StartCoroutine(string methodName);
//
// Summary:
// Starts a coroutine named methodName.
//
// Parameters:
// methodName:
//
// value:
[WrapperlessIcall]
public Coroutine StartCoroutine(string methodName, object value);
[WrapperlessIcall]
public Coroutine StartCoroutine_Auto(IEnumerator routine);
//
// Summary:
// Stops all coroutines running on this behaviour.
[WrapperlessIcall]
public void StopAllCoroutines();
public void StopCoroutine(Coroutine routine);
//
// Summary:
// Stops the first coroutine named methodName, or the coroutine stored in routine
// running on this behaviour.
//
// Parameters:
// methodName:
// Name of coroutine.
//
// routine:
// Name of the function in code.
public void StopCoroutine(IEnumerator routine);
//
// Summary:
// Stops the first coroutine named methodName, or the coroutine stored in routine
// running on this behaviour.
//
// Parameters:
// methodName:
// Name of coroutine.
//
// routine:
// Name of the function in code.
[WrapperlessIcall]
public void StopCoroutine(string methodName);
}
}
  • yield; The coroutine will continue after all Update functions have been called on the next frame.
    yield:协程在所有的Update函数于下一帧调用后继续执行。
  • yield WaitForSeconds(2); Continue after a specified time delay, after all Update functions have been called for the frame
    yield WaitForSeconds(2):在一个指定的时间延迟后继续执行,在所有的Update函数被调用之后。
  • yield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scripts
    yield WaitForFixedUpdate():在所有脚本上所有的FixedUpdate被调用之后继续执行。
  • yield WWW Continue after a WWW download has completed.
    yield WWW:在WWW加载完成之后继续执行。
  • yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
    yield StartCoroutine(MyFunc):用于链接协程,此将等待MyFunc协程完成先

yield  WaitForEndOfFrame();  用于本帧渲染完

05-04 06:46