DCDC/Assets/Scripts/NetworkVisibilityHandler.cs
2026-02-22 15:24:28 +01:00

63 lines
1.9 KiB
C#

using System;
using Fusion;
using UnityEngine;
public class NetworkVisibilityHandler : NetworkBehaviour
{
private ApplicationManager _applicationManager;
// The networked variable that syncs the state to everyone
[Networked, OnChangedRender(nameof(OnVisibilityChanged))]
public bool IsVisible { get; set; }
// References to what we want to hide
private MeshRenderer[] _renderers;
private Collider[] _colliders;
// private void Awake()
// {
public override void Spawned()
{
_applicationManager = FindFirstObjectByType<ApplicationManager>();
if (_applicationManager == null)
{
Debug.LogError("[DCDC] ApplicationManager component not found.");
}
// Find all visuals and colliders on this object
_renderers = GetComponentsInChildren<MeshRenderer>();
_colliders = GetComponentsInChildren<Collider>();
IsVisible = false;
// Apply the initial state (Hidden)
UpdateVisibilityState();
}
// public override void FixedUpdateNetwork()
// {
// // Only the Host can change this variable
// if (_applicationManager.IsHost == true)
// {
// if (OVRInput.GetDown(OVRInput.Button.Two, OVRInput.Controller.RTouch))
// {
// IsVisible = true; // This change will automatically sync to clients
// }
// }
// }
// This method runs whenever 'IsVisible' changes on the network
private void UpdateVisibilityState()
{
var state = IsVisible;
// Toggle Renderers (Visuals)
foreach (var r in _renderers) r.enabled = state;
// Toggle Colliders (Physics/Grabbing)
foreach (var c in _colliders) c.enabled = state;
}
// Boilerplate to link the OnChanged event
private void OnVisibilityChanged() => UpdateVisibilityState();
}