internal static void OnLocalClientSpawnSceneObject(NetworkConnection conn, SpawnSceneObjectMessage msg) { if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null) { localObject.OnSetLocalVisibility(true); } }
internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn) { if (identity.serverOnly) return; if (LogFilter.Debug) Debug.Log("Server SendSpawnMessage: name=" + identity.name + " sceneId=" + identity.sceneId.ToString("X") + " netid=" + identity.netId); // for easier debugging // 'identity' is a prefab that should be spawned if (identity.sceneId == 0) { SpawnPrefabMessage msg = new SpawnPrefabMessage { netId = identity.netId, assetId = identity.assetId, position = identity.transform.position, rotation = identity.transform.rotation, scale = identity.transform.localScale, // serialize all components with initialState = true payload = identity.OnSerializeAllSafely(true) }; // conn is != null when spawning it for a client if (conn != null) { conn.Send(msg); } // conn is == null when spawning it for the local player else { SendToReady(identity, msg); } } // 'identity' is a scene object that should be spawned again else { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage { netId = identity.netId, sceneId = identity.sceneId, position = identity.transform.position, rotation = identity.transform.rotation, scale = identity.transform.localScale, // include synch data payload = identity.OnSerializeAllSafely(true) }; // conn is != null when spawning it for a client if (conn != null) { conn.Send(msg); } // conn is == null when spawning it for the local player else { SendToReady(identity, msg); } } }
internal static void OnSpawnSceneObject(NetworkConnection conn, SpawnSceneObjectMessage msg) { if (LogFilter.Debug) { Debug.Log("Client spawn scene handler instantiating [netId:" + msg.netId + " sceneId:" + msg.sceneId + " pos:" + msg.position); } if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null) { // this object already exists (was in the scene) localObject.Reset(); ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId); return; } NetworkIdentity spawnedId = SpawnSceneObject(msg.sceneId); if (spawnedId == null) { Debug.LogError("Spawn scene object not found for " + msg.sceneId.ToString("X") + " SpawnableObjects.Count=" + spawnableObjects.Count); // dump the whole spawnable objects dict for easier debugging foreach (KeyValuePair <ulong, NetworkIdentity> kvp in spawnableObjects) { Debug.Log("Spawnable: SceneId=" + kvp.Key + " name=" + kvp.Value.name); } return; } if (LogFilter.Debug) { Debug.Log("Client spawn for [netId:" + msg.netId + "] [sceneId:" + msg.sceneId + "] obj:" + spawnedId.gameObject.name); } spawnedId.Reset(); ApplySpawnPayload(spawnedId, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId); }
internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn) { if (identity.serverOnly) { return; } if (LogFilter.Debug) { Debug.Log("Server SendSpawnMessage: name=" + identity.name + " sceneId=" + identity.sceneId + " netid=" + identity.netId); } // for easier debugging // 'identity' is a prefab that should be spawned if (identity.sceneId == 0) { SpawnPrefabMessage msg = new SpawnPrefabMessage(); msg.netId = identity.netId; msg.assetId = identity.assetId; msg.position = identity.transform.position; msg.rotation = identity.transform.rotation; // serialize all components with initialState = true msg.payload = identity.OnSerializeAllSafely(true); // conn is != null when spawning it for a client if (conn != null) { conn.Send((short)MsgType.SpawnPrefab, msg); } // conn is == null when spawning it for the local player else { SendToReady(identity.gameObject, (short)MsgType.SpawnPrefab, msg); } } // 'identity' is a scene object that should be spawned again else { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage(); msg.netId = identity.netId; msg.sceneId = identity.sceneId; msg.position = identity.transform.position; // note: no msg.rotation here because client&server both start with same scene (=same scene object rotations) // include synch data msg.payload = identity.OnSerializeAllSafely(true); // conn is != null when spawning it for a client if (conn != null) { conn.Send((short)MsgType.SpawnSceneObject, msg); } // conn is == null when spawning it for the local player else { SendToReady(identity.gameObject, (short)MsgType.SpawnSceneObject, msg); } } }
static void OnLocalClientSpawnSceneObject(NetworkMessage netMsg) { SpawnSceneObjectMessage msg = netMsg.ReadMessage<SpawnSceneObjectMessage>(); NetworkIdentity localObject; if (NetworkIdentity.spawned.TryGetValue(msg.netId, out localObject) && localObject != null) { localObject.OnSetLocalVisibility(true); } }
internal static void OnSpawnSceneObject(NetworkConnection _, SpawnSceneObjectMessage msg) { if (LogFilter.Debug) { Debug.Log("Client spawn scene handler instantiating [netId:" + msg.netId + " sceneId:" + msg.sceneId + " pos:" + msg.position); } // owner? if (msg.owner) { OnSpawnMessageForOwner(msg.netId); } if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null) { // this object already exists (was in the scene) localObject.Reset(); ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId); #if MIRROR_PROFILING NetworkProfiler.RecordMessage(NetworkDirection.Incoming, typeof(SpawnSceneObjectMessage), localObject.gameObject.name, 1); #endif return; } NetworkIdentity spawnedId = SpawnSceneObject(msg.sceneId); if (spawnedId == null) { Debug.LogError("Spawn scene object not found for " + msg.sceneId.ToString("X") + " SpawnableObjects.Count=" + spawnableObjects.Count); // dump the whole spawnable objects dict for easier debugging if (LogFilter.Debug) { foreach (KeyValuePair <ulong, NetworkIdentity> kvp in spawnableObjects) { Debug.Log("Spawnable: SceneId=" + kvp.Key + " name=" + kvp.Value.name); } } return; } if (LogFilter.Debug) { Debug.Log("Client spawn for [netId:" + msg.netId + "] [sceneId:" + msg.sceneId + "] obj:" + spawnedId.gameObject.name); } #if MIRROR_PROFILING NetworkProfiler.RecordMessage(NetworkDirection.Incoming, typeof(SpawnSceneObjectMessage), spawnedId.gameObject.name, 1); #endif spawnedId.Reset(); spawnedId.pendingOwner = msg.owner; ApplySpawnPayload(spawnedId, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId); }
static void OnLocalClientSpawnSceneObject(NetworkMessage netMsg) { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage(); netMsg.ReadMessage(msg); NetworkIdentity localObject; if (s_NetworkScene.GetNetworkIdentity(msg.netId, out localObject)) { localObject.OnSetLocalVisibility(true); } }
static void OnSpawnSceneObject(NetworkMessage netMsg) { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage(); netMsg.ReadMessage(msg); if (LogFilter.logDebug) { Debug.Log("Client spawn scene handler instantiating [netId:" + msg.netId + " sceneId:" + msg.sceneId + " pos:" + msg.position); } NetworkIdentity localNetworkIdentity; if (s_NetworkScene.GetNetworkIdentity(msg.netId, out localNetworkIdentity)) { // this object already exists (was in the scene) localNetworkIdentity.Reset(); ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, localNetworkIdentity.gameObject); return; } NetworkIdentity spawnedId = SpawnSceneObject(msg.sceneId); if (spawnedId == null) { if (LogFilter.logError) { Debug.LogError("Spawn scene object not found for " + msg.sceneId + " SpawnableObjects.Count=" + s_SpawnableObjects.Count); // dump the whole spawnable objects dict for easier debugging foreach (var kvp in s_SpawnableObjects) { Debug.Log("Spawnable: SceneId=" + kvp.Key + " name=" + kvp.Value.name); } } return; } if (LogFilter.logDebug) { Debug.Log("Client spawn for [netId:" + msg.netId + "] [sceneId:" + msg.sceneId + "] obj:" + spawnedId.gameObject.name); } spawnedId.Reset(); ApplySpawnPayload(spawnedId, msg.position, msg.payload, msg.netId, spawnedId.gameObject); }
internal static void SendSpawnMessage(NetworkIdentity uv, NetworkConnection conn) { if (uv.serverOnly) { return; } if (LogFilter.Debug) { Debug.Log("Server SendSpawnMessage: name=" + uv.name + " sceneId=" + uv.sceneId + " netid=" + uv.netId); } // for easier debugging // 'uv' is a prefab that should be spawned if (uv.sceneId == 0) { SpawnPrefabMessage msg = new SpawnPrefabMessage(); msg.netId = uv.netId; msg.assetId = uv.assetId; msg.position = uv.transform.position; msg.rotation = uv.transform.rotation; // serialize all components with initialState = true NetworkWriter writer = new NetworkWriter(); uv.OnSerializeAllSafely(writer, true); msg.payload = writer.ToArray(); // conn is != null when spawning it for a client if (conn != null) { conn.Send((short)MsgType.SpawnPrefab, msg); } // conn is == null when spawning it for the local player else { SendToReady(uv.gameObject, (short)MsgType.SpawnPrefab, msg); } } // 'uv' is a scene object that should be spawned again else { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage(); msg.netId = uv.netId; msg.sceneId = uv.sceneId; msg.position = uv.transform.position; // include synch data NetworkWriter writer = new NetworkWriter(); uv.OnSerializeAllSafely(writer, true); msg.payload = writer.ToArray(); // conn is != null when spawning it for a client if (conn != null) { conn.Send((short)MsgType.SpawnSceneObject, msg); } // conn is == null when spawning it for the local player else { SendToReady(uv.gameObject, (short)MsgType.SpawnSceneObject, msg); } } }
internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn) { if (identity.serverOnly) { return; } if (LogFilter.Debug) { Debug.Log("Server SendSpawnMessage: name=" + identity.name + " sceneId=" + identity.sceneId.ToString("X") + " netid=" + identity.netId); // for easier debugging } // serialize all components with initialState = true // (can be null if has none) // convert to ArraySegment to avoid reader allocations // (need to handle null case too) ArraySegment <byte> segment = identity.OnSerializeAllSafely(true, out NetworkWriter serialized) ? serialized.ToArraySegment() : default; // 'identity' is a prefab that should be spawned if (identity.sceneId == 0) { SpawnPrefabMessage msg = new SpawnPrefabMessage { netId = identity.netId, owner = conn?.playerController == identity, assetId = identity.assetId, // use local values for VR support position = identity.transform.localPosition, rotation = identity.transform.localRotation, scale = identity.transform.localScale, payload = segment }; // conn is != null when spawning it for a client if (conn != null) { conn.Send(msg); } // conn is == null when spawning it for the local player else { SendToReady(identity, msg); } } // 'identity' is a scene object that should be spawned again else { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage { netId = identity.netId, owner = conn?.playerController == identity, sceneId = identity.sceneId, // use local values for VR support position = identity.transform.localPosition, rotation = identity.transform.localRotation, scale = identity.transform.localScale, payload = segment }; // conn is != null when spawning it for a client if (conn != null) { conn.Send(msg); } // conn is == null when spawning it for the local player else { SendToReady(identity, msg); } } if (serialized != null) { NetworkWriterPool.Recycle(serialized); } }