返回介绍

MonoBehaviour.StartCoroutine 开始协同程序

发布于 2019-12-18 15:38:04 字数 6655 浏览 1273 评论 0 收藏 0

JavaScript => public function StartCoroutine(routine: IEnumerator): Coroutine;
C# => public Coroutine StartCoroutine(IEnumerator routine);

Description 描述

Starts a coroutine.

开始协同程序。

The execution of a coroutine can be paused at any point using the yield statement. The yield return value specifies when the coroutine is resumed. Coroutines are excellent when modelling behaviour over several frames. Coroutines have virtually no performance overhead. StartCoroutine function always returns immediately, however you can yield the result. This will wait until the coroutine has finished execution.

一个协同程序在执行过程中,可以在任意位置使用yield语句。yield的返回值控制何时恢复协同程序向下执行。协同程序在对象自有帧执行过程中堪称优秀。协同程序在性能上没有更多的开销。StartCoroutine函数是立刻返回的,但是yield可以延迟结果。直到协同程序执行完毕。

When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.

用javascript不需要添加StartCoroutine,编译器将会替你完成,但是在C#下,你必须调用StartCoroutine。

JavaScript:

// In this example we show how to invoke a coroutine and continue executing
// the function in parallel.
 
function Start() 
{ 
	// - After 0 seconds, prints "Starting 0.0" 
	// - After 0 seconds, prints "Before WaitAndPrint Finishes 0.0" 
	// - After 2 seconds, prints "WaitAndPrint 2.0" 
	print ("Starting " + Time.time); 
	// Start function WaitAndPrint as a coroutine. And continue execution while it is running 
 
	// this is the same as WaitAndPrint(2.0) as the compiler does it for you automatically 
	StartCoroutine(WaitAndPrint(2.0)); 
	print ("Before WaitAndPrint Finishes " + Time.time); 
}
 
	function WaitAndPrint (waitTime : float) 
{ 
	// suspend execution for waitTime seconds 
	yield WaitForSeconds (waitTime); 
	print ("WaitAndPrint "+ Time.time); 
} 

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Start() {
        print("Starting " + Time.time);
        StartCoroutine(WaitAndPrint(2.0F));
        print("Before WaitAndPrint Finishes " + Time.time);
    }
    IEnumerator WaitAndPrint(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("WaitAndPrint " + Time.time);
    }
}

另一个例子:

JavaScript:

// In this example we show how to invoke a coroutine and wait until it 
// is completed
 
function Start() {
	// - After 0 seconds, prints "Starting 0.0"
	// - After 2 seconds, prints "WaitAndPrint 2.0"
	// - After 2 seconds, prints "Done 2.0"
	print ("Starting " + Time.time);
	// Start function WaitAndPrint as a coroutine. And wait until it is completed.
	// the same as yield WaitAndPrint(2.0);
	yield StartCoroutine(WaitAndPrint(2.0));
	print ("Done " + Time.time);
}
 
function WaitAndPrint (waitTime : float) 
{ 
	// suspend execution for waitTime seconds 
	yield WaitForSeconds (waitTime); 
	print ("WaitAndPrint "+ Time.time); 
} 

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    IEnumerator Start() {
        print("Starting " + Time.time);
        yield return StartCoroutine(WaitAndPrint(2.0F));
        print("Done " + Time.time);
    }
    IEnumerator WaitAndPrint(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("WaitAndPrint " + Time.time);
    }
}

JavaScript => public function StartCoroutine(methodName: string, value: object = null): Coroutine;
C# => public Coroutine StartCoroutine(string methodName, object value = null);

Description 描述

Starts a coroutine named methodName.

开始一个叫methodName方法的协同程序.

In most cases you want to use the StartCoroutine variation above. However StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name. The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.

很多情况下,我们会用到StartCoroutine的一个变体。使用有字符串方法名的StartCoroutine允许你用StopCoroutine去停止它。其缺点就是会有较高的性能开销,而且你只能传递一个参数。

JavaScript:

// In this example we show how to invoke a coroutine using a string name and stop it
 
function Start () 
{ 
	StartCoroutine("DoSomething", 2.0); 
	yield WaitForSeconds(1); 
	StopCoroutine("DoSomething"); 
}
 
function DoSomething (someParameter : float) 
{ 
	while (true) 
	{ 
		print("DoSomething Loop"); 
		// Yield execution of this coroutine and return to the main loop until next frame 
		yield; 
	}
} 

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    IEnumerator Start() {
        StartCoroutine("DoSomething", 2.0F);
        yield return new WaitForSeconds(1);
        StopCoroutine("DoSomething");
    }
    IEnumerator DoSomething(float someParameter) {
        while (true) {
            print("DoSomething Loop");
            yield return null;
        }
    }
}

MonoBehaviour

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文