Last active
November 8, 2021 05:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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