fgm24/Assets/Scripts/Multiplayer/ZDisablerV2.cs

55 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
public class ZDisablerV2 : NetworkBehaviour
{
[Tooltip("Keep enabled if is [BLANK]")]
public KeepIf KeepEnabledIf = KeepIf.IsOwner;
[Tooltip("Add all the objects to disable on join, if not owner")]
public UnityEngine.Object[] objectsToDisable;
public override void OnNetworkSpawn()
{
// Run if networked
if (NetworkManager.Singleton == null) return;
if (KeepEnabledIf == KeepIf.IsOwner && IsOwner) return;
if (KeepEnabledIf == KeepIf.IsServer && IsServer) return;
for (int i = 0; i < objectsToDisable.Length; i++)
{
var objectType = objectsToDisable[i]; // Apparently, "typeof" is not needed /shrug
switch (objectType)
{
case Behaviour monoBehaviour:
monoBehaviour.enabled = false;
break;
case GameObject gameObject:
gameObject.SetActive(false);
break;
case Rigidbody rigidbody:
rigidbody.isKinematic = true;
break;
default:
Debug.LogError($"Can't disable: {objectType.name} (not supported)", objectType);
break;
}
}
}
// Keep enabled if is [blank]
public enum KeepIf
{
IsServer,
IsOwner
}
}