Pregunta

i have found similar questions, but I was not able to solve my problem with the provided answers.

I have the following code, that is supposed to fade between colors in an array.

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;
    }

The function "FadeMaterialColorTo" itself is working fine, but i see no results when calling it with the top function... I have tried dropping the yield in line 3 to get"return (FadeMaterialColorTo(m, colors[i%2], speed, type));" but then i get the following error :

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

Here is a similar topic, but in Unity a return type IEnumerator> does not work

The non-generic type `System.Collections.IEnumerator' cannot be used with the type arguments
¿Fue útil?

Solución

I believe you want is something like:

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;
}

IIRC, something like yield return somefunction() will yield only once if you have another nested yield inside somefunction(), as you do with the yield return null in the body of your while loop.

Otros consejos

Here is another alternative way:

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top