2013-09-28 3 views
1

Я нашел похожие вопросы, но я не смог решить свою проблему с предоставленными ответами.Ошибка возврата возвращаемого значения 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» сама по себе работает нормально, но я не вижу результатов при вызове его с верхней функцией ... Я попытался уронить выход в строке 3, чтобы получить "возврата (FadeMaterialColorTo (м, цвета [i% 2], скорость, тип)); но затем я получаю следующее сообщение об ошибке:

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

Here аналогичная тема, но в единстве типа возвращения IEnumerator> не работает

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

ответ

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

IIRC, что-то вроде yield return somefunction() даст только один раз, если у вас есть еще один вложенный доход внутри somefunction(), как и в случае с yield return null в тело вашего цикла while.

+1

Спасибо, что сделал трюк. StartCoroutine не может быть вызван из статического метода. Я нашел обходное решение [здесь] (http://forum.unity3d.com/attachment.php?attachmentid=46803&d=1363320165), которое было взято из [здесь] (http://forum.unity3d.com/threads/15524- Static-StartCoroutine) – user2572309

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; 
} 
Смежные вопросы