Il nocciolo del mio codice è il seguente:Hai problemi refactoring un metodo IEnumerator con più rendimenti
// Play the first beat
audio.PlayOneShot(beat);
// Show 1st heartbeat border flash
TweenAlpha.Begin(heartbeatPanel.gameObject, 0.1f, currentStress);
yield return new WaitForSeconds(0.1f);
TweenAlpha.Begin(heartbeatPanel.gameObject, 0.5f, 0);
yield return new WaitForSeconds(interval);
// Play the second beat
audio.PlayOneShot(beat);
// Show 2nd heartbeat border flash
TweenAlpha.Begin(heartbeatPanel.gameObject, 0.1f, currentStress);
yield return new WaitForSeconds(0.1f);
TweenAlpha.Begin(heartbeatPanel.gameObject, 0.5f, 0);
yield return new WaitForSeconds(interval * 2);
Ora voglio dividere il codice sopra in un unico metodo IEnumerator con 2 chiamate.
Questo è ciò che mi si avvicinò con:
StartCoroutine(PlayBeat(currentStress, interval));
StartCoroutine(PlayBeat(currentStress, interval * 2));
// ...
IEnumerator PlayBeat(float currentStress, float interval)
{
audio.PlayOneShot(beat);
TweenAlpha.Begin(heartbeatPanel.gameObject, 0.1f, currentStress);
yield return new WaitForSeconds(0.1f);
TweenAlpha.Begin(heartbeatPanel.gameObject, 0.5f, 0);
yield return new WaitForSeconds(interval);
}
Il problema di questo è che invece delle battute suonare con loro corretto intervallo, entrambe battute sembrava allo stesso tempo e perché ho queste chiamate nei un ciclo infinito, Unity si è schiantato perché gli intervalli non vengono considerati.
Qual è il modo migliore per estrarre i miei due blocchi ripetitivi di codice sopra in un singolo metodo IEnumerator?
Il tuo codice attuale sembra buono (almeno, senza vedere 'StartCoroutine') - qual è il problema? –
Puoi essere più specifico di "non funziona" quando descrivi il problema. – ChrisF
Ho ragione a sentire i due ritmi contemporaneamente? – Kay