Пример #1
0
        // local authority client sends sync message to server for broadcasting
        // note: message is registered in NetworkServer.RegisterMessageHandlers
        //       because internal messages can't be registered from the outside
        // TODO make this a [Command] later (Weaver can't weave this as long as
        //      it's still in the same DLL)
        public static void OnClientToServerSync(NetworkMessage netMsg)
        {
            // read message
            TransformMessage message = netMsg.ReadMessage <TransformMessage>();

            // find that NetworkIdentity
            NetworkIdentity identity;

            if (!NetworkIdentity.spawned.TryGetValue(message.netId, out identity))
            {
                Debug.LogError("Received NetworkTransform data for GameObject that doesn't exist");
                return;
            }

            NetworkTransformBase[] foundSyncs = identity.GetComponents <NetworkTransformBase>();
            if (foundSyncs == null || foundSyncs.Length == 0 || message.componentIndex > foundSyncs.Length - 1)
            {
                Debug.LogError("HandleTransform null target");
                return;
            }

            NetworkTransformBase foundSync = foundSyncs[message.componentIndex];

            if (!foundSync.localPlayerAuthority)
            {
                Debug.LogError("HandleTransform no localPlayerAuthority");
                return;
            }

            if (netMsg.conn.clientOwnedObjects == null)
            {
                Debug.LogError("HandleTransform object not owned by connection");
                return;
            }

            if (netMsg.conn.clientOwnedObjects.Contains(message.netId))
            {
                // deserialize payload
                NetworkReader reader = new NetworkReader(message.payload);
                foundSync.DeserializeFromReader(reader);

                // server-only mode does no interpolation to save computations,
                // but let's set the position directly
                if (foundSync.isServer && !foundSync.isClient)
                {
                    foundSync.ApplyPositionAndRotation(foundSync.goal.position, foundSync.goal.rotation);
                }

                // set dirty so that OnSerialize broadcasts it
                foundSync.SetDirtyBit(1UL);
            }
            else
            {
                Debug.LogWarning("HandleTransform netId:" + message.netId + " is not for a valid player");
            }
        }
Пример #2
0
        public void SerializeIntoWriterTest()
        {
            var writer   = new NetworkWriter();
            var position = new Vector3(1, 2, 3);
            var rotation = new Quaternion(0.1f, 0.2f, 0.3f, 0.4f);
            var scale    = new Vector3(0.5f, 0.6f, 0.7f);

            NetworkTransformBase.SerializeIntoWriter(writer, position, rotation, scale);
            var reader = new NetworkReader(writer.ToArray());

            Assert.That(reader.ReadVector3(), Is.EqualTo(position));
            Assert.That(reader.ReadQuaternion(), Is.EqualTo(rotation));
            Assert.That(reader.ReadVector3(), Is.EqualTo(scale));
        }