void Update()
        {
            // if server then always sync to others.
            if (isServer)
            {
                // just use OnSerialize via SetDirtyBit only sync when position
                // changed. set dirty bits 0 or 1
                SetDirtyBit(HasMovedOrRotated() ? 1UL : 0UL);
            }

            // no 'else if' since host mode would be both
            if (isClient)
            {
                // send to server if we have local authority (and aren't the server)
                // -> only if connectionToServer has been initialized yet too
                if (!isServer && hasAuthority)
                {
                    // check only each 'syncInterval'
                    if (Time.time - lastClientSendTime >= syncInterval)
                    {
                        if (HasMovedOrRotated())
                        {
                            // serialize
                            // local position/rotation for VR support
                            NetworkWriter writer = NetworkWriterPool.GetPooledWriter();
                            SerializeIntoWriter(writer, targetComponent.transform.localPosition, targetComponent.transform.localRotation, compressRotation);

                            // send to server
                            CmdClientToServerSync(writer.ToArraySegment());
                            NetworkWriterPool.Recycle(writer);
                        }
                        lastClientSendTime = Time.time;
                    }
                }

                // apply interpolation on client for all players
                // unless this client has authority over the object. could be
                // himself or another object that he was assigned authority over
                if (!hasAuthority)
                {
                    // received one yet? (initialized?)
                    if (goal != null)
                    {
                        // teleport or interpolate
                        if (NeedsTeleport())
                        {
                            // local position/rotation for VR support
                            ApplyPositionAndRotation(goal.localPosition, goal.localRotation);
                        }
                        else
                        {
                            // local position/rotation for VR support
                            ApplyPositionAndRotation(InterpolatePosition(start, goal, targetComponent.transform.localPosition),
                                                     InterpolateRotation(start, goal, targetComponent.transform.localRotation));
                        }
                    }
                }
            }
        }
        void CheckSendRate()
        {
            if (sendMessagesAllowed && syncInterval != 0 && sendTimer < Time.time)
            {
                sendTimer = Time.time + syncInterval;

                NetworkWriter writer = NetworkWriterPool.GetPooledWriter();
                if (WriteParameters(writer))
                {
                    SendAnimationParametersMessage(writer.ToArraySegment());
                }
                NetworkWriterPool.Recycle(writer);
            }
        }
Esempio n. 3
0
        public static NetworkWriter PackWriter <T>(T message) where T : IMessageBase
        {
            NetworkWriter packWriter = NetworkWriterPool.GetPooledWriter();

            // write message type
            int msgType = GetId <T>();

            packWriter.Write((ushort)msgType);

            // serialize message into writer
            message.Serialize(packWriter);

            // return byte[]
            return(packWriter);
        }
Esempio n. 4
0
        public static byte[] PackMessage(int msgType, MessageBase msg)
        {
            NetworkWriter packWriter = NetworkWriterPool.GetPooledWriter();

            // write message type
            packWriter.Write((short)msgType);

            // serialize message into writer
            msg.Serialize(packWriter);

            // return byte[]
            byte[] data = packWriter.ToArray();
            NetworkWriterPool.Recycle(packWriter);
            return(data);
        }
Esempio n. 5
0
        // pack message before sending
        public static byte[] Pack <T>(T message) where T : IMessageBase
        {
            NetworkWriter packWriter = NetworkWriterPool.GetPooledWriter();

            // write message type
            int msgType = GetId <T>();

            packWriter.Write((ushort)msgType);

            // serialize message into writer
            message.Serialize(packWriter);

            // return byte[]
            byte[] data = packWriter.ToArray();
            NetworkWriterPool.Recycle(packWriter);
            return(data);
        }
        void FixedUpdate()
        {
            if (!sendMessagesAllowed)
            {
                return;
            }

            CheckSendRate();

            if (!CheckAnimStateChanged(out int stateHash, out float normalizedTime))
            {
                return;
            }

            NetworkWriter writer = NetworkWriterPool.GetPooledWriter();

            WriteParameters(writer);

            SendAnimationMessage(stateHash, normalizedTime, writer.ToArraySegment());
            NetworkWriterPool.Recycle(writer);
        }