unity C# 嵌套 IEnumerator [英] Unity C# Nested IEnumerator

查看:49
本文介绍了unity C# 嵌套 IEnumerator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我嵌套了 IEnumerator 方法,例如:

In my code I have nested IEnumerator methods, such like this :

    private IEnumerator PerformRequest(string url) {

        // Doing stuff

        UnityWebRequest request = UnityWebRequest.Get(url);
        yield return request.SendWebRequest();

        // Doing stuff
    }

    private IEnumerator PerformRequest2(string url) {

        // Doing stuff

        return PerformRequest(url);

        // Doing stuff
    }

    public IEnumerator PerformRequest3(string url) {

        // Doing stuff

        return PerformRequest2(url);

        // Doing stuff
    }

我想知道如果我在上层方法中添加 yield 有什么区别,例如:

I am wondering what is the difference if I add yield in upper level methods, such like this :

    private IEnumerator PerformRequest(string url) {

        // Doing stuff

        UnityWebRequest request = UnityWebRequest.Get(url);
        yield return request.SendWebRequest();

        // Doing stuff
    }

    private IEnumerator PerformRequest2(string url) {

        // Doing stuff

        yield return PerformRequest(url);

        // Doing stuff
    }

    public IEnumerator PerformRequest3(string url) {

        // Doing stuff

        yield return PerformRequest2(url);

        // Doing stuff
    }

有区别吗,还是一样的行为?

Is there a difference, or is it the same behaviour ?

谢谢!

推荐答案

当然可以

我自己之前做过这样的事情,你绝对可以产生一个它本身产生的方法,因为发生的情况是执行完全是线性的 并且当 PerformRequest 产生并返回一些值(Web 请求,但实际上并不重要)时,该值会传播到 中的 yield return...PerformRequest2 然后向上传播到 PerformRequest3.Unity 将在正确的位置恢复执行就好了(您也可以通过测试来回答您自己的问题;).

Sure, this works

I've done something like this myself before, you can absolutely yield a method that it itself yeilds, because what happens is that execution is completely linear and when PerformRequest yields and returns some value (the web request, but not actually important) that propagates up to the yield return... in PerformRequest2 which then propagates upwards to PerformRequest3. Unity will resume execuction at the correct location just fine (and you could have answered your own question by testing it, too ;).

基本上它与任何其他嵌套函数相同,例如

Basically its the same as any other nested function, e.g

int GetValue() {
    return 4;
}

int GetValue2() {
    return GetValue();
}

执行完全相同(只是类型不同):

Is executed exactly the same as (just with a different type):

IEnumerator GetValue() {
    return new SomeIEnumerator();
}

IEnumerator GetValue2() {
    return GetValue();
}

转换为协程产生(yield 只是一个特殊的关键字,它不影响返回类型!):

Which converts to coroutine yielding (yield is just a special keyword, it doesn't effect return type!):

IEnumerator GetValue() {
    yield return new SomeIEnumerator();
}

IEnumerator GetValue2() {
    yield return GetValue();
}

你也可以这样做,但我通常在其中找不到任何价值:

You could also do it like this, but I generally don't find any value in it:

IEnumerator GetValue() {
    yield return new SomeIEnumerator();
}

IEnumerator GetValue2() {
    //other stuff, with yield so our function still returns a value
    StarCoroutine(GetValue());
    //this stuff runs without waiting
}

Adassko 还发现了这个很棒的教程关于协程和嵌套协程.

Adassko also found this great tutorial about coroutines and nested coroutines.

这篇关于unity C# 嵌套 IEnumerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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