Skip to main content

UI controls

A Toggle switches a lamp on and off and a Slider picks its colour, for everyone. Each control's On Value Changed calls the script, which takes ownership and syncs the new value. Other players apply it in OnDeserialization and move their own controls with SetIsOnWithoutNotify and SetValueWithoutNotify, so the change doesn't echo back.

LampControls.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// A Toggle switches a lamp on and off and a Slider picks its colour, for everyone (Toggle → On Value Changed →
/// LampControls.OnToggle, Slider → On Value Changed → LampControls.OnSlider). The player who changes a control takes
/// ownership and syncs the values; everyone else moves their controls to match without raising the events again.
/// </summary>
public class LampControls : StageTimeBehaviour
{
public Toggle powerToggle;
public Slider hueSlider;
public Renderer lampBulb;
public TMP_Text label;

[Synced] private bool on = true;
[Synced] private float hue = 0.12f;

private void Start() { Apply(); }

public void OnToggle()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
on = powerToggle.isOn;
Apply();
}

public void OnSlider()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
hue = hueSlider.value;
Apply();
}

public override void OnDeserialization() { Apply(); }

private void Apply()
{
if (powerToggle != null) powerToggle.SetIsOnWithoutNotify(on);
if (hueSlider != null) hueSlider.SetValueWithoutNotify(hue);
Color colour = Color.HSVToRGB(hue, 0.8f, 1f);
if (lampBulb != null)
{
lampBulb.enabled = on;
lampBulb.material.color = colour;
lampBulb.material.SetColor("_EmissionColor", colour * 2f);
}
if (label != null) label.text = (on ? "On" : "Off") + " hue " + hue.ToString("0.00");
}
}