Pass ForgeDocs Product page

Scripting

Transitions and scene changes

Change scene with a transition

PassForgeTransition.LoadScene covers the screen, loads the scene and uncovers it, in one call. The scene must be in the build's scene list.

using PassForge.Effects;
using UnityEngine;

// A level exit with a trigger collider: covers the screen, loads the scene behind it and uncovers it, in one call.
public class LevelExit : MonoBehaviour
{
    [SerializeField] string m_Scene = "Level 2";   // listed in File > Build Profiles, under Scene List

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player") && !PassForgeTransition.isPlaying)
            PassForgeTransition.LoadScene(m_Scene, TransitionStyle.Iris, 0.5f);
    }
}

A loading screen of your own

Cover, load while covered, then reveal. yield return PassForgeTransition.WaitUntilCovered() waits until the cover is fully on; it returns the same object every time, so waiting allocates nothing. PassForgeTransition.WaitUntilRevealed waits for the other end.

using System.Collections;
using PassForge.Effects;
using UnityEngine;
using UnityEngine.SceneManagement;

// A loading screen of your own: cover, load while covered, reveal. Put it on a root object.
public class LoadingScreen : MonoBehaviour
{
    public void Load(string scene) => StartCoroutine(LoadUnderCover(scene));

    IEnumerator LoadUnderCover(string scene)
    {
        DontDestroyOnLoad(gameObject);   // so this coroutine outlives the scene it started in

        PassForgeTransition.Cover(TransitionStyle.Fade, 0.4f);
        yield return PassForgeTransition.WaitUntilCovered();   // real time, so it works while paused

        yield return SceneManager.LoadSceneAsync(scene);

        // A cover stays on through a scene load until something reveals it.
        PassForgeTransition.Reveal(TransitionStyle.Fade, 0.4f);
        Destroy(gameObject);
    }
}

React when the screen is covered

PassForgeTransition.onCovered is raised once a cover is fully on, and PassForgeTransition.onRevealed once the scene shows again. They are static, so subscribe in OnEnable and unsubscribe in OnDisable.

using PassForge.Effects;
using UnityEngine;

// Swaps two rooms behind a cover with no coroutine: told by onCovered when the screen is hidden, and by onRevealed
// when it shows again.
public class RoomSwap : MonoBehaviour
{
    [SerializeField] GameObject m_From;
    [SerializeField] GameObject m_To;

    bool m_Swapping;

    // The events are static, so subscribe while enabled only.
    void OnEnable()
    {
        PassForgeTransition.onCovered += OnCovered;
        PassForgeTransition.onRevealed += OnRevealed;
    }

    void OnDisable()
    {
        PassForgeTransition.onCovered -= OnCovered;
        PassForgeTransition.onRevealed -= OnRevealed;
    }

    public void Swap()
    {
        m_Swapping = true;
        PassForgeTransition.Cover(TransitionStyle.Diamonds, 0.5f);
    }

    void OnCovered()
    {
        if (!m_Swapping)
            return;   // a cover something else asked for

        m_From.SetActive(false);
        m_To.SetActive(true);
        PassForgeTransition.Reveal(TransitionStyle.Diamonds, 0.5f);
    }

    void OnRevealed() => m_Swapping = false;
}

Clear a cover the last scene left on

A Death moment's fade to black stays on through a scene load. In a scene that never covers the screen itself, call PassForgeTransition.RevealLeftoverCover from Start; with nothing covered, it does nothing.

using PassForge.Effects;
using UnityEngine;

// In a scene that never covers the screen itself, such as a menu reached from a Death moment's fade to black.
public class RevealOnStart : MonoBehaviour
{
    void Start()
    {
        // Waits a few frames and for any transition under way, and does nothing when nothing is covered.
        PassForgeTransition.RevealLeftoverCover(TransitionStyle.Fade, 0.4f);
    }
}

Open a level with an iris

PassForgeTransition.focus makes the Iris open on a point in the world. A Reveal with no cover before it starts fully covered.

using System.Collections;
using PassForge.Effects;
using UnityEngine;

// Opens the level on the player with an iris.
public class LevelIntro : MonoBehaviour
{
    [SerializeField] Transform m_Player;

    IEnumerator Start()
    {
        // A Reveal with no cover before it starts fully covered. The iris opens on the point given as focus.
        PassForgeTransition.focus = m_Player.position;
        PassForgeTransition.Reveal(TransitionStyle.Iris, 1.1f);

        // The first frames compile shaders, which in a browser can outlast the whole reveal on the real-time
        // clock: open the iris again once frames come steadily.
        yield return null;
        yield return null;

        PassForgeTransition.focus = m_Player.position;
        PassForgeTransition.Reveal(TransitionStyle.Iris, 1.1f);
    }
}