void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } NetIncomingMessage message; while ((message = peer.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)message.ReadByte(); Debug.Log("status " + status); switch (status) { case NetConnectionStatus.Connected: Vector3 spawnPosition = Vector3.zero; Quaternion spawnRotation = Quaternion.identity; MyNetwork.SpawnPlayer(spawnPosition, spawnRotation); NetOutgoingMessage newMessage = peer.CreateMessage(); newMessage.Write((byte)NetDataType.SPAWN_PLAYER); newMessage.Write(spawnPosition); newMessage.Write(spawnRotation); NetSendResult result = peer.SendMessage(newMessage, message.SenderConnection, NetDeliveryMethod.ReliableUnordered); Debug.Log("sent: " + result); break; } break; case NetIncomingMessageType.WarningMessage: Debug.LogWarning(message.ReadString()); break; case NetIncomingMessageType.DebugMessage: Debug.Log(message.ReadString()); break; case NetIncomingMessageType.ConnectionApproval: { // TODO: Store connections message.SenderConnection.Approve(); break; } default: print("unhandled message with type: " + message.MessageType); break; } } }
void OnDataType(NetDataType type, NetIncomingMessage message) { switch (type) { case NetDataType.SPAWN_PLAYER: Vector3 position = message.ReadVector3(); Quaternion rotation = message.ReadQuaternion(); MyNetwork.SpawnPlayer(position, rotation); break; } }