Skip to content

Connect UI to context switching

A context switcher selects the visible presentation, but it does not automatically retarget every UI component. Bind the UI's command context and its optional presentation scope together.

Setup

  1. Configure and validate the context switcher.
  2. Keep the Canvas and binding component outside switchable presentation/content roots so they stay enabled.
  3. Use On Context Changed to call TunalUIRuntimeController.SetContext with the event context.
  4. Also select the matching TunalPresentationGroup for TunalUguiContext. For separately positioned rigs, use the switcher's ActivePresentation; do not assume the group is a child of the simulation context.
  5. Test switching while the time, cloud, and persistence panels are open.

The supplied SetPresentationGroupFromContext convenience method searches the context's children. That is useful for a combined hierarchy, but it cannot discover an unrelated detached root. Explicit active-group binding avoids that assumption:

SwitcherUiBinding.cs
using Sital.Tunal.Core;
using Sital.Tunal.UI;
using UnityEngine;

namespace TunalExamples
{
    public sealed class SwitcherUiBinding : MonoBehaviour
    {
        [SerializeField] private TunalContextSwitcher switcher;
        [SerializeField] private TunalUIRuntimeController commands;
        [SerializeField] private TunalUguiContext views;

        private void OnEnable()
        {
            if (switcher == null) return;
            switcher.onContextChanged.AddListener(Bind);
            if (switcher.IsInitialized) Bind(switcher.ActiveContext);
        }

        private void OnDisable()
        {
            if (switcher != null) switcher.onContextChanged.RemoveListener(Bind);
        }

        private void Bind(TunalContext context)
        {
            if (commands != null) commands.SetContext(context);
            if (views != null)
            {
                views.SetController(commands);
                views.SetPresentationGroup(switcher.ActivePresentation);
            }
        }
    }
}

Create this component in your scripts folder, keep its GameObject active, and assign all three references. Use this component or equivalent persistent event bindings; avoid wiring two independent binders that compete for selection. It subscribes on enable and removes its listener on disable. Querying the current selection handles a binder enabled after the startup event.

TunalUguiContextSwitcher.SetSwitcher selects the switcher used by the context-list view; Rebuild recreates options after authoring changes. Its clock display refresh interval is separate from simulation advancement. Selecting an option changes presentation through the switcher; other contexts continue simulating.

Saving with Whole World mode continues to target the registered world, not only the highlighted option. A loaded state can update a UI display without changing which presentation entry is selected. Treat simulation restoration and visual selection as separate operations.

Related: Runtime UI, World persistence, binding rules.