如何与A&QUOT此功能;产量"具体工作? [英] How does this function with a "yield" work in detail?

查看:170
本文介绍了如何与A&QUOT此功能;产量"具体工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个方法(统一C#脚本中),但我不明白的收益部分实际上是如何工作的。



我从MSDN知道该函数会返回一个IEnumerator,我可以throught遍历,但是这个代码等待1.5秒,不会被重复,因为这将意味着,中创建的对象的创建多次。
任何人在这里谁可以给我解释一下这段代码是如何工作的?



 的IEnumerator DestroyShip()
{
//创建新的游戏对象
实例化(ExplosionPrefab,transform.position,transform.rotation);
//使当前游戏物体隐形
gameObject.renderer.enabled = FALSE;
//设置当前游戏对象
transform.position =新的位置的Vector3新(0F,transform.position.y,transform.position.z);
//等待1,5秒
收益回报新WaitForSeconds(1.5F);
//将当前游戏对象再次可见
gameObject.renderer.enabled = TRUE;
}


解决方案

枚举器,编译器生成您正在重复。一旦



,编译器会生成实现IEnumerator的,其中有一个的MoveNext()函数和当前属性的类。类将所有存储调用之间功能的状态所需要的成员。具体细节可以被认为是编译魔术师。



这生成的类的对象,将由Unity3d引擎处理和管理。该Unity3d引擎将调用的MoveNext()上每个活动协程每帧(除非另有指示)。



这使Unity3d程序员编写播放出一帧脚本在一个时间。 。的C#编译器魔术和Unity3d引擎的魔法效果非常强大,但是易使用的脚本组合



要回答你的问题:该代码在函数将被执行一次,但它会在'收率返回'语句暂停



如上所述,实现的IEnumerator一个特殊目的是通过C#编译器创建的。



在第一次调用的MoveNext(),您的函数创建一个爆炸,并将当前对象的新WaitForSeconds(1.5F)。



Unity3d引擎检查这个物体,看到它是特殊类的一个实例WaitForSeconds,所以把普查员的一些等待队列,直到1.5秒过去了就不会问的第二个元素。在此期间,许多帧将被渲染和爆炸将播放



1.5秒后,统一将再次GRAP从队列枚举,并调用的MoveNext() 。的函数的第二部分现在将执行,但将无法产生的第二个目的。的MoveNext()将返回false以表明它未能获得新的元素,这是信号Unity3d抛出此枚举了。垃圾收集器会回收在某些时候内存在时间



至于说:大量的编译器和Unity3d魔术是怎么回事。只要你记住,你的功能将被搁置,直​​到在每个yield return语句下一帧,你就会知道,足以从这些特殊功能中受益。


I got this method (inside a Unity C# Script), but I do not understand how the "yield" part actually works.

I know from the MSDN that the function will return an IEnumerator which I could iterate throught, but this code waits 1,5 seconds and does not get iterated because this would mean, the objects created inside were created multiple times. Anyone here who can explain me how this code works?

IEnumerator DestroyShip()
{
    // create new gameobject
    Instantiate(ExplosionPrefab, transform.position, transform.rotation);
    // make current gameobject invisible
    gameObject.renderer.enabled = false;
    // set new position for the current gameobject
    transform.position = new Vector3(0f, transform.position.y, transform.position.z);
    // wait for 1,5 seconds 
    yield return new WaitForSeconds(1.5f);
    // make the current gameobject visible again
    gameObject.renderer.enabled = true;
}

解决方案

The Enumerator the compiler generates for you is being iterated. Once.

The compiler will generate a class that implements IEnumerator, which has a MoveNext() function and a Current property. The class will have all the members required to store the state of the function between calls. The exact details can be considered "Compiler Magic".

The object of this generated class, will be handled and managed by the Unity3d Engine. The Unity3d Engine will call MoveNext() on each active coroutine once every frame (unless instructed otherwise).

This enabled the Unity3d Programmer to write scripts that are played out one frame at a time. A combination of C# compiler magic and Unity3d Engine magic results in very-powerful-but-easy-to-use scripting.

To answer your question: the code in your function will be executed once, but it will pause at the 'yield return' statement.

As stated above, a special object that implements IEnumerator is created by the C# compiler.

On the first call to MoveNext(), your function creates an explosion and sets the current object to "new WaitForSeconds(1.5f)".

The Unity3d engine inspects this object, sees it is an instance of the special class "WaitForSeconds" so puts the enumerator on some waiting queue, and won't ask for the second element until 1.5 sec have passed. In the meantime, many frames will be rendered and the explosion will be played.

After 1.5 sec, Unity will grap the enumerator from the queue, and call MoveNext() again. The second part of your function will execute now, but will fail to generate a second object. MoveNext() will return false to indicate it failed to get a new element, which is the signal to Unity3d to throw this enumerator away. The Garbage Collector will reclaim the memory at some point in time.

As said: lots of compiler and Unity3d magic is going on. As long as you remember that your function will be put on hold till the next frame at each yield return statement, you'll know enough to benefit from those special functions.

这篇关于如何与A&QUOT此功能;产量"具体工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆