예제 #1
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);
            }
        }
예제 #2
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);
                }
            }
        }
예제 #3
0
        internal static void HandleChildTransform(NetworkMessage netMsg)
        {
            LocalChildTransformMessage message = netMsg.ReadMessage <LocalChildTransformMessage>();

            GameObject foundObj = NetworkServer.FindLocalObject(message.netId);

            if (foundObj == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Received NetworkTransformChild data for GameObject that doesn't exist");
                }
                return;
            }
            var children = foundObj.GetComponents <NetworkTransformChild>();

            if (children == null || children.Length == 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleChildTransform no children");
                }
                return;
            }
            if (message.childIndex >= children.Length)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleChildTransform childIndex invalid");
                }
                return;
            }

            NetworkTransformChild foundSync = children[message.childIndex];

            if (foundSync == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleChildTransform null target");
                }
                return;
            }
            if (!foundSync.localPlayerAuthority)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleChildTransform no localPlayerAuthority");
                }
                return;
            }

            if (!netMsg.conn.clientOwnedObjects.Contains(message.netId))
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("NetworkTransformChild netId:" + message.netId + " is not for a valid player");
                }
                return;
            }

            foundSync.UnserializeModeTransform(new NetworkReader(message.payload), false);
            foundSync.m_LastClientSyncTime = Time.time;

            if (!foundSync.isClient)
            {
                // dedicated server wont interpolate, so snap.
                foundSync.m_Target.localPosition = foundSync.m_TargetSyncPosition;
                foundSync.m_Target.localRotation = foundSync.m_TargetSyncRotation3D;
            }
        }