using Simuspaces.Simulation; public class DirectionalLightSwitch : SimuScriptBase { // Paste/slot the Directional Light reference here. public SVLight DirectionalLight; // Paste/slot the button or switch object here. public SceneObject Button; // Intensity used when the light is switched on. public float OnIntensity = 1f; // Interaction settings. public bool HighlightButton = true; public float InteractDistance = 10f; // Default state is off. private bool isOn = false; public override void Start() { base.Start(); RegisterButton(null); // Force the starting state to off. ApplyLightState(null); } public override void OnUserJoin(SVUserInfo user) { string username = user != null ? user.Username : null; // Make the button available to the joining user. RegisterButton(username); // Give the joining user the current light state. ApplyLightState(username); } private void RegisterButton(string targetUsername) { if (Button == null || Button.IsNull) { Log("[Light Switch] Slot a SceneObject into Button."); return; } MakeInteractable( Button, OnButtonClicked, HighlightButton, targetUsername, true, InteractDistance, "Toggle light" ); } private void OnButtonClicked(SVInteractionInfo info) { if (DirectionalLight == null || DirectionalLight.IsNull) { Log("[Light Switch] Slot a Directional Light into DirectionalLight."); return; } isOn = !isOn; // No username means the change is sent to everyone. ApplyLightState(null); Log("[Light Switch] Light is now " + (isOn ? "ON" : "OFF")); } private void ApplyLightState(string targetUsername) { if (DirectionalLight == null || DirectionalLight.IsNull) return; float intensity = isOn ? Mathf.Max(0f, OnIntensity) : 0f; // Keep the Light component enabled and use intensity as the switch. SetLightEnabled(DirectionalLight, true, targetUsername); SetLightIntensity(DirectionalLight, intensity, targetUsername); } }