Tutoriel : la sortie : charger le niveau suivant

En entrant dans la sortie, le niveau suivant se charge en arrière-plan pendant qu'une barre se remplit, puis s'affiche quand il est prêt.

1. Préparation

Préparation
  1. Mettez le script sur un objet avec un Trigger3D à la fin du niveau.
  2. Ajoutez un panneau d'interface nommé Loading avec une barre de progression nommée Loading Bar.
  3. Dans File > Build Settings, ajoutez les deux niveaux à la liste des scènes.

2. Le script

Le script

Scripts/LevelExit.cs

using System.Collections;

public class LevelExit : Behaviour
{
    public string nextScene = "Level 2";

    bool leaving;

    public override void Start() { UI.SetVisible("Loading", false); }

    public override void OnTriggerEnter(GameObject other)
    {
        if (leaving || !other.CompareTag("Player")) return;
        leaving = true;
        Events.Emit("level done");
        StartCoroutine(Leave());
    }

    IEnumerator Leave()
    {
        UI.SetVisible("Loading", true);
        SceneLoad load = Scene.LoadAsync(nextScene);
        // the scene is read while this one goes on; it is not shown until we say so
        load.allowSceneActivation = false;
        while (load.progress < 0.9f)
        {
            UI.SetValue("Loading Bar", load.progress);
            yield return null;
        }
        UI.SetValue("Loading Bar", 1f);
        yield return new WaitForSeconds(0.3f);
        load.allowSceneActivation = true;
    }
}

Enregistrez-le sous Scripts/LevelExit.cs.

3. Comment ça marche

Comment ça marche

Charger en arrière-plan signifie que le jeu ne se fige pas pendant le chargement du niveau suivant : la barre montre la progression, et le nouveau niveau apparaît quand il est prêt. Réglez le nom du niveau suivant dans l'Inspector.

Suite : un menu des commandes.