using System.Linq; using QSB.Messaging; using QSB.Utility; using UnityEngine.Networking; namespace QSB.Events { /// /// Client-only-side component for managing player leaves. /// public class PlayerLeave : NetworkBehaviour { private MessageHandler _leaveHandler; private void Awake() { _leaveHandler = new MessageHandler(MessageType.Leave); _leaveHandler.OnClientReceiveMessage += OnClientReceiveMessage; } public void Leave(uint playerId, uint[] objectIds) { var message = new LeaveMessage { SenderId = playerId, ObjectIds = objectIds }; _leaveHandler.SendToAll(message); } private void OnClientReceiveMessage(LeaveMessage message) { var playerName = PlayerRegistry.GetPlayer(message.SenderId).Name; DebugLog.ToAll(playerName, "disconnected."); PlayerRegistry.RemovePlayer(message.SenderId); foreach (var objectId in message.ObjectIds) { DestroyObject(objectId); } } private void DestroyObject(uint objectId) { var component = FindObjectsOfType() .FirstOrDefault(x => x.netId.Value == objectId); if (component == null) { return; } var transformSync = component.GetComponent(); if (transformSync != null) { Destroy(transformSync.SyncedTransform.gameObject); } Destroy(component.gameObject); } } }