Ejemplo n.º 1
0
        /// <summary>
        /// Creates a transform message which includes the specified data.
        /// </summary>
        /// <param name="clientMessage">Indicates if this is a client message.</param>
        public static byte[] createTransformMessage(NetworkIdentity netIdentity, NetworkTransform.TransformSpace transformSpace,
                                                    bool sendPosition = true, bool sendRotation = true, bool sendScale = true, bool clientMessage = false)
        {
            List <byte> msgBytes = new List <byte>();

            byte msgHeader = (byte)(clientMessage ? 1 : 0);       //Change message header to indicate if it's a client message or not

            msgBytes.Add(msgHeader);                              //Message Header

            msgBytes.AddRange(netIdentity.networkID.serialize()); //Identity of the transform on the network

            bool worldSpace = transformSpace == NetworkTransform.TransformSpace.World;

            //Position
            if (sendPosition)
            {
                msgBytes.AddRange(((byte)1).serialize()); //Signal position is being sent

                Vector3 pos = worldSpace ? netIdentity.transform.position : netIdentity.transform.localPosition;
                msgBytes.AddRange(pos.x.serialize());
                msgBytes.AddRange(pos.y.serialize());
                msgBytes.AddRange(pos.z.serialize());
            }
            else
            {
                msgBytes.AddRange(((byte)0).serialize()); //Signal no position is being sent
            }
            //Rotation
            if (sendRotation)
            {
                msgBytes.AddRange(((byte)1).serialize()); //Signal rotation is being sent

                Vector3 rot = worldSpace ? netIdentity.transform.eulerAngles : netIdentity.transform.localEulerAngles;
                msgBytes.AddRange(rot.x.serialize());
                msgBytes.AddRange(rot.y.serialize());
                msgBytes.AddRange(rot.z.serialize());
            }
            else
            {
                msgBytes.AddRange(((byte)0).serialize()); //Signal no rotation is being sent
            }
            //Scale
            if (sendScale)
            {
                msgBytes.AddRange(((byte)1).serialize()); //Signal scale is being sent
                msgBytes.AddRange(netIdentity.transform.localScale.x.serialize());
                msgBytes.AddRange(netIdentity.transform.localScale.y.serialize());
                msgBytes.AddRange(netIdentity.transform.localScale.z.serialize());
            }
            else
            {
                msgBytes.AddRange(((byte)0).serialize()); //Signal no scale is being sent
            }
            if (clientMessage)
            {
                msgBytes.Add((byte)(worldSpace ? 1 : 0)); //Send transform space flag
            }
            return(msgBytes.ToArray());
        }
Ejemplo n.º 2
0
        private static void processTransformMessage(byte[] msg, bool clientMessage)
        {
            short netID = ObjectSerializer.deserializeShort(ref msg);

            if (NetworkEntityManager.Instance.findComponent(netID, out NetworkTransform networkTransform)) //NetworkTransform found
            {
                bool receivedPos = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedPos) //Position data was sent
                {
                    float posX = ObjectSerializer.deserializeFloat(ref msg);
                    float posY = ObjectSerializer.deserializeFloat(ref msg);
                    float posZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Position = new Vector3(posX, posY, posZ);
                }

                bool receivedRot = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedRot) //Rotation data was sent
                {
                    float rotX = ObjectSerializer.deserializeFloat(ref msg);
                    float rotY = ObjectSerializer.deserializeFloat(ref msg);
                    float rotZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Rotation = Quaternion.Euler(new Vector3(rotX, rotY, rotZ));
                }

                bool receivedScale = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedScale) //Scale data was sent
                {
                    float scaleX = ObjectSerializer.deserializeFloat(ref msg);
                    float scaleY = ObjectSerializer.deserializeFloat(ref msg);
                    float scaleZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Scale = new Vector3(scaleX, scaleY, scaleZ);
                }

                //Message was sent from a client, now being processed on the server
                if (clientMessage && ServerBehaviour.Instance)
                {
                    //What transform space is this data in? (World / Local)
                    bool  worldSpace = ObjectSerializer.deserializeByte(ref msg) == 1;
                    short clientID   = ObjectSerializer.deserializeShort(ref msg); //Client who sent the message
                    NetworkTransform.TransformSpace transformSpace = worldSpace ? NetworkTransform.TransformSpace.World : NetworkTransform.TransformSpace.Local;
                    byte[] transformMsg = MessageFactory.createTransformMessage(networkTransform.networkIdentity, transformSpace, receivedPos, receivedRot, receivedScale);
                    ServerBehaviour.Instance.sendMessage(transformMsg, clientID); //Forward to all clients but the sender
                }
            }
        }