IEnumerator yield返回类型错误

时间:2013-09-28 13:32:04

标签: unity3d return-value ienumerator

我发现了类似的问题,但我无法用提供的答案解决我的问题。

我有以下代码,它应该在数组中的颜色之间淡出。

public static IEnumerator FadeMaterialColors(Material m, Color[] colors, float speed, ProgressCurve type){
    for (int i = 0; i < colors.Length; i++){
        yield return (FadeMaterialColorTo(m, colors[i%2], speed, type));
    }
    yield return null;
}

public static IEnumerator FadeMaterialColorTo(Material m, Color target, float duration, ProgressCurve type){
        Color start = m.color;
        float y, t = Time.time;
        float progress = (Time.time - t)/duration;

        while (progress < 1f){
            y = GetProgressCurve(progress, type);
            m.color = start + y*(target - start);
            yield return null; // return here next frame
            progress = (Time.time - t)/duration;
        }
        m.color = target;
    }

函数“FadeMaterialColorTo”本身工作正常,但是当用top函数调用它时我看不到任何结果...我试过在第3行中降低yield得到“return(FadeMaterialColorTo(m,colors [i%] 2],速度,类型));“但后来我得到以下错误:

Cannot implicitly convert type `System.Collections.IEnumerator' to `bool'

Here是一个类似的主题,但在Unity中,返回类型IEnumerator&gt;不起作用

The non-generic type `System.Collections.IEnumerator' cannot be used with the type arguments

2 个答案:

答案 0 :(得分:0)

我相信你想要的是:

public static IEnumerator FadeMaterialColors(Material m, Color[] colors, float speed,
ProgressCurve type){
    for (int i = 0; i < colors.Length; i++){
        yield return StartCoroutine(FadeMaterialColorTo(m, colors[i%2], speed, type));
    }
    yield return null;
}
如果你在yield return somefunction()内有另一个嵌套产量,那么像somefunction()这样的东西只会产生一次,就像你yield return null循环体内的while一样

答案 1 :(得分:0)

这是另一种替代方式:

public static IEnumerator FadeMaterialColors(Material m, Color[] colors, float speed, ProgressCurve type){
    for (int i = 0; i < colors.Length; i++){
        IEnumerator process = FadeMaterialColorTo(m, colors[i%2], speed, type);
        while(process.MoveNext())
            yield return null;
    }
    yield return null;
}
相关问题