Tutorial: un menú de pausa

Escape detiene el juego y muestra un panel con tres botones: continuar, empezar de nuevo o salir.

1. Construye el panel

Construye el panel
  1. Añade un panel de interfaz llamado Pause Panel.
  2. Añade tres botones dentro. En el Inspector pon su action en resume, restart y quit.

2. El script

El script

Scripts/PauseMenu.cs

public class PauseMenu : Behaviour
{
    bool paused;

    public override void Start() { Set(false); }

    public override void Update(float deltaTime)
    {
        if (Input.GetKeyDown("Escape")) Set(!paused);
        if (!paused) return;
        // (a button reports the frame it was clicked, by the name of its action)
        if (UI.Clicked("resume")) Set(false);
        if (UI.Clicked("restart")) { Set(false); Scene.Reload(); }
        if (UI.Clicked("quit")) Application.Quit();
    }

    void Set(bool on)
    {
        paused = on;
        // time stands still while the panel is up: physics, animations and Update's deltaTime all stop
        Time.timeScale = on ? 0f : 1f;
        UI.SetVisible("Pause Panel", on);
        Cursor.visible = on;
    }
}

Guárdalo como Scripts/PauseMenu.cs y añádelo a cualquier objeto.

3. Cómo funciona

Cómo funciona

4. Pruébalo

Pruébalo

Pulsa PLAY y luego Escape. El panel aparece y el juego se congela; resume continúa desde el mismo momento.

Siguiente: guardar, cargar y una mejor puntuación.