示例#1
0
 internal static void OnLocalClientSpawnPrefab(NetworkConnection conn, SpawnPrefabMessage msg)
 {
     if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
     {
         localObject.OnSetLocalVisibility(true);
     }
 }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        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);
                }
            }
        }
示例#4
0
        static void OnLocalClientSpawnPrefab(NetworkMessage netMsg)
        {
            SpawnPrefabMessage msg = netMsg.ReadMessage<SpawnPrefabMessage>();

            NetworkIdentity localObject;
            if (NetworkIdentity.spawned.TryGetValue(msg.netId, out localObject) && localObject != null)
            {
                localObject.OnSetLocalVisibility(true);
            }
        }
示例#5
0
        static void OnLocalClientSpawnPrefab(NetworkMessage netMsg)
        {
            SpawnPrefabMessage msg = new SpawnPrefabMessage();

            netMsg.ReadMessage(msg);

            NetworkIdentity localObject;

            if (s_NetworkScene.GetNetworkIdentity(msg.netId, out localObject))
            {
                localObject.OnSetLocalVisibility(true);
            }
        }
示例#6
0
        static void OnSpawnPrefab(NetworkMessage netMsg)
        {
            SpawnPrefabMessage msg = new SpawnPrefabMessage();

            netMsg.ReadMessage(msg);

            if (!msg.assetId.IsValid())
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
                }
                return;
            }
            if (LogFilter.logDebug)
            {
                Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + "]");
            }

            NetworkIdentity localNetworkIdentity;

            if (s_NetworkScene.GetNetworkIdentity(msg.netId, out localNetworkIdentity))
            {
                // this object already exists (was in the scene), just apply the update to existing object
                localNetworkIdentity.Reset();
                ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, null);
                return;
            }

            GameObject    prefab;
            SpawnDelegate handler;

            if (NetworkScene.GetPrefab(msg.assetId, out prefab))
            {
                var obj = (GameObject)Object.Instantiate(prefab, msg.position, msg.rotation);
                if (LogFilter.logDebug)
                {
                    Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
                }

                localNetworkIdentity = obj.GetComponent <NetworkIdentity>();
                if (localNetworkIdentity == null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity");
                    }
                    return;
                }
                localNetworkIdentity.Reset();
                ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, obj);
            }
            // lookup registered factory for type:
            else if (NetworkScene.GetSpawnHandler(msg.assetId, out handler))
            {
                GameObject obj = handler(msg.position, msg.assetId);
                if (obj == null)
                {
                    if (LogFilter.logWarn)
                    {
                        Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
                    }
                    return;
                }
                localNetworkIdentity = obj.GetComponent <NetworkIdentity>();
                if (localNetworkIdentity == null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Client object spawned for " + msg.assetId + " does not have a network identity");
                    }
                    return;
                }
                localNetworkIdentity.Reset();
                localNetworkIdentity.SetDynamicAssetId(msg.assetId);
                ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, obj);
            }
            else
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
                }
            }
        }
示例#7
0
        internal static void OnSpawnPrefab(NetworkConnection conn, SpawnPrefabMessage msg)
        {
            if (msg.assetId == Guid.Empty)
            {
                Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
                return;
            }
            if (LogFilter.Debug)
            {
                Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + "]");
            }

            if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
            {
                // this object already exists (was in the scene), just apply the update to existing object
                localObject.Reset();
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
                return;
            }

            if (GetPrefab(msg.assetId, out GameObject prefab))
            {
                GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);
                if (LogFilter.Debug)
                {
                    Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
                }

                localObject = obj.GetComponent <NetworkIdentity>();
                if (localObject == null)
                {
                    Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity");
                    return;
                }
                localObject.Reset();
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
            }
            // lookup registered factory for type:
            else if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler))
            {
                GameObject obj = handler(msg.position, msg.assetId);
                if (obj == null)
                {
                    Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
                    return;
                }
                localObject = obj.GetComponent <NetworkIdentity>();
                if (localObject == null)
                {
                    Debug.LogError("Client object spawned for " + msg.assetId + " does not have a network identity");
                    return;
                }
                localObject.Reset();
                localObject.assetId = msg.assetId;
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
            }
            else
            {
                Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
            }
        }
示例#8
0
        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);
                }
            }
        }
示例#9
0
        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);
            }
        }
示例#10
0
        static void OnSpawnPrefab(NetworkMessage netMsg)
        {
            SpawnPrefabMessage msg = netMsg.ReadMessage<SpawnPrefabMessage>();

            if (msg.assetId == Guid.Empty)
            {
                Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
                return;
            }
            if (LogFilter.Debug) { Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + "]"); }

            NetworkIdentity localObject;
            if (NetworkIdentity.spawned.TryGetValue(msg.netId, out localObject) && localObject != null)
            {
                // this object already exists (was in the scene), just apply the update to existing object
                localObject.Reset();
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.payload, msg.netId);
                return;
            }

            GameObject prefab;
            SpawnDelegate handler;
            if (GetPrefab(msg.assetId, out prefab))
            {
                GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);

                // Avoid "(Clone)" suffix. some games do show the name. no need for an extra sync to fix the suffix.
                obj.name = prefab.name;

                if (LogFilter.Debug)
                {
                    Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
                }

                localObject = obj.GetComponent<NetworkIdentity>();
                if (localObject == null)
                {
                    Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity");
                    return;
                }
                localObject.Reset();
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.payload, msg.netId);
            }
            // lookup registered factory for type:
            else if (spawnHandlers.TryGetValue(msg.assetId, out handler))
            {
                GameObject obj = handler(msg.position, msg.assetId);
                if (obj == null)
                {
                    Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
                    return;
                }
                localObject = obj.GetComponent<NetworkIdentity>();
                if (localObject == null)
                {
                    Debug.LogError("Client object spawned for " + msg.assetId + " does not have a network identity");
                    return;
                }
                localObject.Reset();
                localObject.SetDynamicAssetId(msg.assetId);
                ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.payload, msg.netId);
            }
            else
            {
                Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
            }
        }