Book Image

Unity 4 Game Development HOTSHOT

By : Jate Wittayabundit
Book Image

Unity 4 Game Development HOTSHOT

By: Jate Wittayabundit

Overview of this book

<p>Immerse yourself in the world of high-end game design by partaking in challenging missions. Start off by working with the Sprite Mode, then learn the basics of creating a UI system for an RPG, and work your way through the game virtually embodying your greatest hero or heroine.</p> <p>Every project is designed to push your Unity skills to the limit and beyond. You will start by creating a 2D platform game with the new 2D sprite feature and move on to the Unity GUI system. Then, you will create a 3D character and make it move. By the end of this book, you will know how to post the player's score to the hi-score board.</p>
Table of Contents (19 chapters)
Unity 4 Game Development HOTSHOT
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

StartCoroutine


StartCoroutine starts a coroutine.

The execution of coroutine can be paused at any point using the yield statement. The yield return value specifies when coroutine is resumed. Coroutines are excellent when modeling behavior over several frames. Coroutines have virtually no performance overhead. The StartCoroutine() function always returns a value immediately; therefore, you can yield the result. This will wait until coroutine has finished execution.

Tip

When using JavaScript, it is not necessary to use StartCoroutine; the compiler will do this for you. However, when writing C# code, you must call StartCoroutine. (For more details, refer to Appendix C, Major Differences Between C# and Unity JavaScript.)

In the following example, we will show how to invoke a coroutine and continue executing the function in parallel:

// JavaScript user:

function Start() {
  // Starting = 0.0
  Debug.Log ("Starting = " + Time.time);
  // StartCoroutine WaitAndPrint (In JavaScript, you can also useWaitAndPrint(5.0) which will get the same result.
  StartCoroutine(WaitAndPrint(5.0)); 
  // Before WaitAndPrint = 5.0
  Debug.Log ("Before WaitAndPrint = " + Time.time);
}

function WaitAndPrint(waitTime : float) {
    //Suspend execution for 5 seconds 
    yield WaitForSeconds(waitTime);
    // WaitAndPrint = 5.0
    Debug.Log ("WaitAndPrint = " + Time.time);
}


// C# user:

void Start() {
  // Starting = 0.0
  Debug.Log ("Starting = " + Time.time);
  StartCoroutine(WaitAndPrint(5.0f)); 
  // Before WaitAndPrint = 5.0
  Debug.Log ("Before WaitAndPrint = " + Time.time);
}

IEnumerator WaitAndPrint(float  waitTime) {
    //Suspend execution for 5 seconds 
    yield return new WaitForSeconds(waitTime);
    // WaitAndPrint = 5.0
    Debug.Log ("WaitAndPrint = " + Time.time);
}

The following example will wait until the WaitAndPrint() function has finished its execution and then continue executing the rest of the code in the Start() function:

// JavaScript user:

function Start() {
  // Starting = 0.0
  Debug.Log ("Starting = " + Time.time);
  // StartCoroutine WaitAndPrint (In JavaScript, you can also useyield WaitAndPrint(5.0) which will get the same result.
  yield StartCoroutine(WaitAndPrint(5.0)); 
  // Done WaitAndPrint = 5.0
  Debug.Log ("Done WaitAndPrint = " + Time.time);
}

function WaitAndPrint(waitTime : float) {
    //Suspend execution for 5 seconds 
    yield WaitForSeconds(waitTime);
    // WaitAndPrint = 5.0
    Debug.Log ("WaitAndPrint = " + Time.time);
}


// C# user:

IEnumerator Start() {
  // Starting = 0.0
  Debug.Log ("Starting = " + Time.time);
  yield return StartCoroutine(WaitAndPrint(5.0f)); 
  // Done WaitAndPrint = 5.0
  Debug.Log ("Done WaitAndPrint = " + Time.time);
}

IEnumerator WaitAndPrint(float  waitTime) {
    //Suspend execution for 5 seconds 
    yield return new WaitForSeconds(waitTime);
    // WaitAndPrint = 5.0
    Debug.Log ("WaitAndPrint = " + Time.time);
}

Using StartCoroutine with method name (string)

In most cases, you want to use the StartCoroutine variation at the start of a code. However, StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name.

Tip

The downside is that the string version has a higher runtime overhead to start coroutine, and you can pass only one parameter.

In the following example, we will see how to invoke coroutine using a string name and stop it:

// JavaScript user:

function Start() {
  // Start Coroutine DoSomething
  StartCoroutine("DoSomething", 5.0); 
  //  Wait for 2 seconds
  yield WaitForSeconds(2.0);
  // Stop Coroutine DoSomething
  StopCoroutine("DoSomething");
}

function DoSomething (someParameter : float) {
  while (true) {
        // DoSomething Loop
        Debug.Log ("DoSomething Loop = " + Time.time);
     // Yield execution of this coroutine and return to the main loop until next frame
           yield;
  }
}


// C# user:

IEnumerator Start() {
  // Start Coroutine DoSomething
  StartCoroutine("DoSomething", 5.0f); 
  //  Wait for 2 seconds
  yield return new WaitForSeconds(2.0f);
  // Stop Coroutine DoSomething
  StopCoroutine("DoSomething");
}

IEnumerator DoSomething (float  someParameter) {
  while (true) {
      // DoSomething Loop
      Debug.Log ("DoSomething Loop = " + Time.time);
  // Yield execution of this coroutine and return to the main 
  loop until next frame
      yield return null;
  }
}