Multiple Contexts and Switching¶
Use multiple ordinary TunalContext instances when environments need separate clocks, dates, profiles, or simulation state. A context switcher then selects which presentation and optional scene content the player sees.
Selecting a context for display does not pause or transfer its simulation. Other enabled contexts continue according to their own clock settings. Switching also does not automatically reassign every reactive object or lifecycle instance in the scene.
Start from the example¶
Open the Multi Context demo to inspect its Earth, Mars, and Fantasy entries. For your own scene, start with two ordinary context/presentation instances rather than the singleton prefab.
Separate switchable presentation roots from the simulation context roots. A conceptual arrangement is:
Scene
Context A TunalContext and its owned systems
Context B TunalContext and its owned systems
Presentation A TunalPresentationGroup and controllers
Presentation B TunalPresentationGroup and controllers
Content A Optional instantiated scene objects
Content B Optional instantiated scene objects
Context Switcher TunalContextSwitcher
This is a layout example, not the exact names required in a prefab. Explicit group assignments remove the need to infer these bindings from sibling names.
Configure the switcher¶
- Add Tunal > Core > Tunal Context Switcher to a GameObject outside all roots it will manage.
- In Contexts, add an entry for each selection.
- Give each entry a clear Display Name, assign its Context, and assign a unique Presentation group.
- If the entry should also show terrain, buildings, or props, add their scene instances to Scene Roots. Leave the list empty if no additional content is needed.
- Choose Starting Context, a zero-based list index.
- Resolve the configuration messages, then enter Play Mode. Use the Inspector's selection controls to test each entry.
Each presentation-group component must be enabled. The switcher activates/deactivates its GameObject hierarchy, not the group component alone. It sets the groups' context bindings explicitly when applying a selection.
Keep the managed roots independent¶
- A presentation root must not contain a
TunalContext; hiding it would otherwise stop that simulation. - Presentation roots must be unique and not nested within each other.
- Optional Scene Roots must be instantiated scene GameObjects, not prefab assets.
- Scene Roots cannot contain contexts, overlap one another, or overlap a presentation hierarchy.
- The switcher must stay outside every root it controls.
These restrictions prevent one selection from accidentally disabling the switcher, another presentation, or a running context. The switcher validates them before applying a selection. Although entries may reuse a simulation context, they still require separate non-overlapping managed roots.
Bind UI and scene consumers¶
On Context Changed publishes the selected context at startup and when the selection changes. The Multi Context demo uses that event to rebind runtime UI and its presentation selection.
Your UI, camera features, or other consumers should deliberately follow that event if they need to follow the visible context. An object explicitly bound to Context A stays bound to A unless your integration changes it. Reactivating content is not the same thing as changing that object's simulation authority.
Code can call SwitchToContext(index) and check its boolean result. SetContext(index) is a UnityEvent-friendly wrapper; NextContext() and PreviousContext() cycle entries. Selection is a Play Mode operation. Re-selecting the already active entry does not emit another change event; RefreshSelection() reapplies its roots and can cause OnDisable/OnEnable callbacks in those hierarchies.
What keeps running?¶
The switcher does not disable, pause, or initialize contexts. A clock can still stop because it was paused or disabled elsewhere. Scene objects under deactivated content roots stop receiving their normal Unity updates; that should not be confused with stopping the owning simulation.
Whether a lifecycle object catches up when re-enabled follows its history and time policies, not a guarantee supplied by context selection.