Avançar para o conteúdo principal

Unity3D Coroutines

Coroutines are functions that maintain status while returning code execution to the method that called them.

They are useful for the development of games to allow the execution of certain code over several frames.

For example to animate the camera of the game we could execute the following code:

for(int i=0;i<100;i++){
    transform.position += new Vector3(1, 0,0);
}

If this code is within a function that is executed normally it is not possible to see the camera moving along the X axis since the game scenario update only occurs at the end of the cycle and not during the execution of this.

In order to see the movement of the camera, it is necessary to perform an iteration of the cycle and then update the scene (rendering) and then continue the cycle with one more iteration and refresh the scene again, in this case what's needed is to interrupt the execution and to resume without losing the state, for this we can implement a coroutine.

A coroutine is just an IEnumerator in C #, the following function runs a cycle with 100 iterations, and at each iteration the camera moves one value along the X axis and then stops running for 0.1 seconds. Execution continues with updating the cycle variable and one more iteration.

IEnumerator animaCameraPrincipal()
    {
        for(int i = 0; i < 100; i++) {
            transform.position += new Vector3(1, 0,0);
            yield return new WaitForSecondsRealtime(0.1f);
        }
    }

This function is executed as follows:

       StartCoroutine(animaCameraPrincipal());

It is very important not to forget that as this function is executed over time it should not be called before its execution is completed. In this case the total execution of the function code lasts 10 seconds (0.1 x 100 = 10).

The coroutine can be stopped in two ways:
      - stopping all coroutines:  StopAllCoroutines();
      - stopping just the one we executed and for that we need to save a reference for the function, like so, Coroutine func=StartCoroutine(animaCameraPrincipal()); and then stopping like so, StopCoroutine(funcao);


Comentários

Mensagens populares deste blogue

New Unity 3D Project

Today I will present a new project that I started. From the post about the car I am building a game with cars, or transportation. The idea is very simple: the player starts with a car and a mission, when he is done with the mission he gets some cash that can be spent buying a new vehicle. Here are some pics: - the car in Unity - the car in Blender - working in the texture - looks great - a wheel - the texture in Gimp - back in Unity testing different materials  like water - and code

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't...

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D. The Unity 3D physics engine is used in order to give the car a real behavior. This are the steps: [1] - Create a new Project