-1

Example for sequential code:

Console.WriteLine("Hello");
Sleep(20);
Console.WriteLine("End");  

Example for loop code(function is being looped):

bool step2 = false;
bool step3 = false;
bool beginn = true;
int i = 0;

void looped() //each second
{
  if (beginn == true)
  {
    Console.WriteLine("Hello");
    beginn = false;
    step2 = true;
  }
  if (step2 == true)
  {
    if (i <= 20)
    {
      i++;
    }
    else
    {
      step2 = false;
      step3 = true;
    }
  }
  if (step3 == true)
  {
    Console.WriteLine("End");
    step3 = false;
  }
}

Which program converts sequential code into loop code? I want to use it for unity, so c#/mono or javascript output is desired.

In general, what are the right terms for each kind of coding?

4

1 回答 1

1

由于这是 Unity,我认为您正在寻找他们的协程和yield WaitForSeconds()范例:http ://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

void Begin()
{
    StartCoroutine("looped");
}

IEnumerator looped() //each second
{
    Console.WriteLine("Hello");
    yield return new WaitForSeconds(20);
    Console.WriteLine("End");
}

编辑:JavaScript 版本:

looped();

function looped()
{
    print("Hello");
    yield WaitForSeconds(20);
    print("End");
}

编辑:如果打算从Update方法中调用它,则需要使用StartCoroutine方法来启动looped

C#:

void Update()
{
    StartCoroutine("looped");
}

IEnumerator looped() //each second
{
    Console.WriteLine("Hello");
    yield return new WaitForSeconds(20);
    Console.WriteLine("End");
}

JavaScript:

function Update()
{
    StartCoroutine("looped")
}

function looped()
{
    print("Hello");
    yield WaitForSeconds(20);
    print("End");
}
于 2013-05-15T11:59:17.700 回答