- Home /
understanding c# yield coroutines
hi, i read many article talking about coroutines using ienumerator and know that its better than update for function that i don't need to check it every frame or like function that only happen once in scene... but i still don't get it how to implement it...
so i have this script that only happen once in the beginning and i put it in update of my game maybe you can translate it into coroutines so i get more understanding about all this thing
timer0 -= Time.deltaTime;
if (timer0 < 0)
{
TweenScale.Begin(black2, .3f, new Vector3(1, 1, 0));
if (black2.transform.localScale.x < 200)
{
TweenAlpha.Begin(black2, .1f, 0);
}
timer1 -= Time.deltaTime;
}
if(timer1 < 0)
StartGame();
so how is it in coroutines?
edit: and i also want to change startgame() into coroutines (its only doing 3..2...1.. ready.... start word that scaling up for sequence)
void StartGame()
{
if (!isGameStart)
{
TweenScale.Begin(start3, .4f, new Vector3(100, 100, 1));
if (start3.transform.localScale.x > 90)
{
TweenAlpha.Begin(start3, .1f, 0);
TweenScale.Begin(start2, .3f, new Vector3(100, 100, 1));
}
if (start2.transform.localScale.x > 80)
{
TweenAlpha.Begin(start2, .1f, 0);
TweenScale.Begin(start1, .3f, new Vector3(120, 120, 1));
}
if (start1.transform.localScale.x > 110)
{
TweenAlpha.Begin(start1, .1f, 0);
TweenScale.Begin(start0, .4f, new Vector3(180, 180, 1));
}
if (start0.transform.localScale.x > 160)
{
TweenAlpha.Begin(start0, .1f, 0);
isGameStart = true;
TweenPosition.Begin(EMPButton, .3f, new Vector3(-100, 104, 1));
}
//dualJoysticks.SetActive(true);
//timer1 = 1;
}
}
and this is after i change into coroutines
IEnumerator readyingGame()
{
while (!isGameStart)
{
TweenScale.Begin(start3, .4f, new Vector3(100, 100, 1));
if (start3.transform.localScale.x > 90)
{
TweenAlpha.Begin(start3, .1f, 0);
TweenScale.Begin(start2, .3f, new Vector3(100, 100, 1));
}
yield return null;
if (start2.transform.localScale.x > 80)
{
TweenAlpha.Begin(start2, .1f, 0);
TweenScale.Begin(start1, .3f, new Vector3(120, 120, 1));
}
yield return null;
if (start1.transform.localScale.x > 110)
{
TweenAlpha.Begin(start1, .1f, 0);
TweenScale.Begin(start0, .4f, new Vector3(180, 180, 1));
}
yield return null;
if (start0.transform.localScale.x > 160)
{
TweenAlpha.Begin(start0, .1f, 0);
TweenPosition.Begin(EMPButton, .3f, new Vector3(-100, 104, 1));
}
yield return null;
isGameStart = true;
}
}
How about this? And ins$$anonymous$$d of checking that in every frame, you should probably call your code in the Start method.
Answer by robertbu · Jul 06, 2014 at 06:50 PM
Here is a fairly literal translation:
IEnumerator DoIt() {
while (timer1 >= 0)
{
timer0 -= Time.deltaTime;
if (timer0 < 0)
{
TweenScale.Begin(black2, .3f, new Vector3(1, 1, 0));
if (black2.transform.localScale.x < 200)
{
TweenAlpha.Begin(black2, .1f, 0);
}
timer1 -= Time.deltaTime;
}
yield return null;
}
StartGame();
}
Here is a bit better translation. Without know how TweenAlpha and TweenScale work, I cannot take it further.
IEnumerator DoIt() {
return new WaitForSeconds(timer0);
while (timer1 >= 0)
{
TweenScale.Begin(black2, .3f, new Vector3(1, 1, 0));
if (black2.transform.localScale.x < 200)
{
TweenAlpha.Begin(black2, .1f, 0);
}
timer1 -= Time.deltaTime;
yield return null;
}
StartGame();
}
wow thanks and i now trying to change startgame() into coroutines (see my edit) to but it stuck in TweenScale.Begin(start3, .4f, new Vector3(100, 100, 1));
Answer by Juice-Tin · Jul 06, 2014 at 06:39 PM
It's basically like a function you can create that instead of just running through immediately, you can make the function wait for lengths of time.
This explains it pretty well. http://docs.unity3d.com/Manual/Coroutines.html
Your answer
