Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Last active November 8, 2021 05:48
Show Gist options
  • Save shivaduke28/81060b6082ba17701c2f64fde1613fce to your computer and use it in GitHub Desktop.
Save shivaduke28/81060b6082ba17701c2f64fde1613fce to your computer and use it in GitHub Desktop.
A simple UdonSharp script for globally synced slider. It works for late joiners, but I think its gameobject has to be active when Scene starts.
using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
namespace VRCTools
{
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class GlobalSlider : UdonSharpBehaviour
{
[SerializeField] Slider slider;
// 1. Add SerializeField for objects that you want to change with slider
// [SerializeField] Light light;
[UdonSynced, FieldChangeCallback(nameof(SyncedValue))]
float syncedValue;
public float SyncedValue
{
get => syncedValue;
set
{
deserializing = true;
if (!Networking.IsOwner(gameObject))
{
slider.value = value;
}
syncedValue = value;
// 2. Use slider's value here
// light.intensity = value;
deserializing = false;
}
}
bool deserializing;
public void OnValueChanged()
{
if (deserializing) return;
Networking.SetOwner(Networking.LocalPlayer, gameObject);
SyncedValue = slider.value;
RequestSerialization();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment