/// <summary> /// Destroys the object. /// </summary> /// <param name="tni">The <see cref="TinyNetIdentity"/> of the object.</param> /// <param name="destroyServerObject">if set to <c>true</c> destroy the object on server too.</param> public void DestroyObject(TinyNetIdentity tni, bool destroyServerObject) { if (TinyNetLogLevel.logDebug) { TinyLogger.Log("DestroyObject instance:" + tni.TinyInstanceID); } /*if (_localIdentityObjects.ContainsKey(tni.TinyInstanceID.NetworkID)) { * _localIdentityObjects.Remove(tni.TinyInstanceID.NetworkID); * }*/ RemoveTinyNetIdentityFromList(tni); if (tni.ConnectionToOwnerClient != null) { tni.ConnectionToOwnerClient.RemoveOwnedObject(tni); } TinyNetObjectDestroyMessage msg = new TinyNetObjectDestroyMessage(); msg.networkID = tni.TinyInstanceID.NetworkID; SendMessageByChannelToAllObserversOf(tni, msg, DeliveryMethod.ReliableOrdered); for (int i = 0; i < tinyNetConns.Count; i++) { tinyNetConns[i].HideObjectToConnection(tni, true); } /*if (TinyNetGameManager.instance.isListenServer) { * tni.OnNetworkDestroy(); * }*/ tni.OnNetworkDestroy(); // when unspawning, dont destroy the server's object if (destroyServerObject) { Object.Destroy(tni.gameObject); } tni.ReceiveNetworkID(new TinyNetworkID(-1)); }
/// <summary> /// Called when an object is destroyed. /// </summary> /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectDestroyMessage"/>.</param> void OnObjectDestroy(TinyNetMessageReader netMsg) { netMsg.ReadMessage(s_TinyNetObjectDestroyMessage); if (TinyNetLogLevel.logDebug) { TinyLogger.Log("TinyNetClient::OnObjDestroy networkID:" + s_TinyNetObjectDestroyMessage.networkID); } TinyNetIdentity localObject = GetTinyNetIdentityByNetworkID(s_TinyNetObjectDestroyMessage.networkID); if (localObject != null) { RemoveTinyNetIdentityFromList(localObject); localObject.OnNetworkDestroy(); if (!TinyNetGameManager.instance.InvokeUnSpawnHandler(localObject.assetGUID, localObject.gameObject)) { // default handling if (localObject.sceneID == 0) { Object.Destroy(localObject.gameObject); } else { // scene object.. disable it in scene instead of destroying localObject.gameObject.SetActive(false); _sceneIdentityObjectsToSpawn[localObject.sceneID] = localObject; } } } else { if (TinyNetLogLevel.logDebug) { TinyLogger.LogWarning("Did not find target for destroy message for " + s_TinyNetObjectDestroyMessage.networkID); } } }