Example #1
0
        private void SpawnEntity(uint peerId, byte[] data)
        {
            var idMsg = new ObjectSpawnMessage();

            idMsg.Deserialize(data);
            GameObject newObj;

            if (IsSceneObject(idMsg, out var prefab))
            {
                // Object is in Scene -> activate it
                newObj = prefab;
                newObj.transform.position = idMsg.Position;
                newObj.transform.rotation = idMsg.Rotation;
                newObj.SetActive(true);
            }
            else if (prefab != null)
            {
                // Object is Prefab -> Instantiate
                newObj = NetworkScene.Spawn(prefab, idMsg.Position, idMsg.Rotation);
            }
            else
            {
                // No Object found with AssetId
                return;
            }
            newObj.GetComponent <SNetIdentity>().Initialize(idMsg.Id);
        }
Example #2
0
        public void SendSpawnMessage(NetworkIdentity uv, GameServerConnection conn)
        {
            if (!uv.serverOnly)
            {
                if (uv.sceneId.IsEmpty())
                {
                    ObjectSpawnMessage objectSpawnMessage = new ObjectSpawnMessage();
                    objectSpawnMessage.netId    = uv.netId;
                    objectSpawnMessage.assetId  = uv.assetId;
                    objectSpawnMessage.position = uv.transform.position;
                    objectSpawnMessage.rotation = uv.transform.rotation;
                    NetworkWriter networkWriter = new NetworkWriter();
                    uv.UNetSerializeAllVars(networkWriter);
                    if (networkWriter.Position > 0)
                    {
                        objectSpawnMessage.payload = networkWriter.ToArray();
                    }

                    if (conn != null)
                    {
                        conn.Send(3, objectSpawnMessage);
                    }
                    else
                    {
                        SendToReady(uv.gameObject, 3, objectSpawnMessage);
                    }
                }
                else
                {
                    ObjectSpawnSceneMessage objectSpawnSceneMessage = new ObjectSpawnSceneMessage();
                    objectSpawnSceneMessage.netId    = uv.netId;
                    objectSpawnSceneMessage.sceneId  = uv.sceneId;
                    objectSpawnSceneMessage.position = uv.transform.position;
                    NetworkWriter networkWriter2 = new NetworkWriter();
                    uv.UNetSerializeAllVars(networkWriter2);
                    if (networkWriter2.Position > 0)
                    {
                        objectSpawnSceneMessage.payload = networkWriter2.ToArray();
                    }

                    if (conn != null)
                    {
                        conn.Send(10, objectSpawnSceneMessage);
                    }
                    else
                    {
                        SendToReady(uv.gameObject, 3, objectSpawnSceneMessage);
                    }
                }
            }
        }
Example #3
0
        private bool IsSceneObject(ObjectSpawnMessage idMsg, out GameObject obj)
        {
            if (idMsg.SceneId.IsValid())
            {
                obj = _sceneObjects.Find(o => o.SceneId == idMsg.SceneId)?.gameObject;
                if (obj != null)
                {
                    return(true);
                }
            }

            if (idMsg.AssetId.IsValid())
            {
                obj = spawnList.Find(go => go.GetComponent <SNetIdentity>()?.AssetId == idMsg.AssetId);
                return(false);
            }

            obj = null;
            return(false);
        }
Example #4
0
        public void Initialize(string id)
        {
            // TODO Initialize Called when Spawn Message is received on the client side
            Register(id);
            if (!IsServer)
            {
                return;
            }

            var trans = transform;
            // TODO Send Spawn Message to Clients
            var msg = new ObjectSpawnMessage
            {
                Id       = id,
                AssetId  = AssetId,
                SceneId  = SceneId,
                Position = trans.position,
                Rotation = trans.rotation
            };

            Debug.Log($"Sending spawn message : {msg.Id}, {msg.AssetId}, {msg.SceneId}");
            NetworkRouter.SendByChannel(ChannelType.SNetIdentity, SNetManager.SpawnMessageHeader, msg);
        }