Skip to content

Creating Your Custom Transition

Collapsed Plug edited this page Nov 30, 2021 · 9 revisions

To get the full potential of UniSwitcher, definitely create a transition effect!

What the Transition Effect for UniSwitcher looks like

The transition effect for UniSwitcher must check all the following boxes.


  • Must be able to handle all four states defined in TransitionState.
  • Is a Canvas GameObject with a script (at the root) that implements ITransitionBackgroundController.
  • Is a prefab.
  • Is referenced from the UniSwitcherInstaller.

Enum TransitionState

In UniSwitcher, the Transition is defined in four states, which is defined in the TransitionState enum.

State Name Description
Ready The Transition is not started, i.e., waiting. This one is the default state.
When the "Out" state ends, it automatically switches to this state.
In Requires a direct interaction to switch to this state.
This is where the previous scene is starting to get covered with the transition screen.
Wait When the "In" state ends, the transition state automatically switches to this state.
The user only sees the transition screen.
Out Requires a direct interaction to switch to this state.
This is where the next scene is starting to appear below the transition screen.

The transition effect must be able to handle those states correctly.
For example, you could have an Animator which has the four states above as its Animator state.

Interface ITransitionBackgroundController

Your transition effect must have a script that implements this interface (and extends MonoBehaviour.)

For an example implementation, check out Assets/Plugins/UniSwitcher/Sample/TransitionBackgroundController.cs.

DetectMainCamera()

When is it called?

When a new Scene is loaded (non-additively.)

What should I do here?

If your Canvas is using Screen space - Camera mode, you need to poll the current Camera.main and update the canvas camera to that one.
Otherwise, you can leave this method as a no-op.

// Your IDE may warn that this is expensive - you may safely suppress the warning because it's only called once per scene change.
var mainCamera = Camera.main;
if (mainCamera != null)
{
    mainCanvas.worldCamera = mainCamera;
    return;
}
Debug.LogWarning("No main camera found for transition object. It might work weirdly");

TriggerTransitionIn<T>(Switcher.SceneTransitionConfiguration<T> config)

When is it called?

When the Transition In is needed.

What should I do here?

Put the Transition into the "In" state by, e.g., calling SetTrigger on the Animator.

TriggerTransitionOut<T>(Switcher.SceneTransitionConfiguration<T> config)

When is it called?

When the Transition Out is needed.

What should I do here?

Put the Transition into the "Out" state.

IsTransitionAllowed()

When is it called?

  • When the Switcher tries to start a scene change.
  • When the Switcher tries to load the next scene.

What should I do here?

This one is super important! This method tells UniSwitcher if it is okay to trigger the following transition effects.
Usually, it is enough that you check if the transition state is Ready or Wait.

return GetTransitionState() == TransitionState.Ready || GetTransitionState() == TransitionState.Wait;

GetTransitionState()

When is it called?

  • Used for IsTransitionAllowed().

What should I do here?

Report the current transition state as TransitionState enum. This probably requires polling your Animator, such as:

var stateHash = transitionAnimator.GetCurrentAnimatorStateInfo(0).fullPathHash;
if (stateHash == Animator.StringToHash("Base Layer.TransitionIn")) {
    return TransitionState.In;
} else if ( // and so on

ForceTransitionWait()

When is it called?

Called for a rare case where you want to force the transition state to Wait state.

[TIP]
You could create a transition in your Animator so that you can handle this case, but this is only called when Switcher.ForceTransitionWait. If you are not using that, you can implement this method as a no-op.

Clone this wiki locally