62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using Fusion;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class NetworkedSlider : NetworkBehaviour
|
|
{
|
|
[SerializeField] private Slider sliderUI; // World Space Slider
|
|
[SerializeField] private Transform sliderFill; // Or a custom visual
|
|
[SerializeField] private ActionsManager actionsManager;
|
|
|
|
// Synced across all clients
|
|
[Networked, OnChangedRender(nameof(OnSliderValueChanged))]
|
|
public float SliderValue { get; set; } = 10.0f;
|
|
|
|
public override void Spawned()
|
|
{
|
|
// Init UI to current network value
|
|
if (sliderUI != null)
|
|
sliderUI.value = SliderValue;
|
|
}
|
|
|
|
// Called when the networked value changes on remote clients
|
|
private void OnSliderValueChanged()
|
|
{
|
|
Debug.Log($"[DCDC] Slider changed to: {SliderValue}");
|
|
if (sliderUI != null)
|
|
sliderUI.value = SliderValue;
|
|
|
|
// Apply whatever the slider controls
|
|
ApplySliderEffect(SliderValue);
|
|
}
|
|
|
|
// Call this from your hand interaction / pinch gesture
|
|
public void SetSliderValue(float newValue)
|
|
{
|
|
if (Object.HasStateAuthority)
|
|
{
|
|
SliderValue = Mathf.Clamp(newValue, 10f, 100f);
|
|
// OnChangedRender doesn't fire locally on state authority, so update UI manually
|
|
if (sliderUI != null)
|
|
sliderUI.value = SliderValue;
|
|
ApplySliderEffect(SliderValue);
|
|
}
|
|
else
|
|
{
|
|
RPC_RequestSliderChange(newValue);
|
|
}
|
|
}
|
|
|
|
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
|
|
private void RPC_RequestSliderChange(float value)
|
|
{
|
|
SliderValue = Mathf.Clamp(value, 10f, 100f);
|
|
}
|
|
|
|
private void ApplySliderEffect(float value)
|
|
{
|
|
Debug.Log($"[DCDC] Slider applied: {value}");
|
|
if (actionsManager != null)
|
|
actionsManager.OnNetworkSliderValueReceived(value);
|
|
}
|
|
} |