コード例 #1
0
        /// <summary>
        /// Creates and returns a SyncVarMessage for the given fieldType if it's valid.
        /// </summary>
        /// <param name="networkBehaviour">NetworkBehaviour managing the SyncVar.</param>
        /// <param name="objType">Type of the SyncVar field.</param>
        /// <param name="fieldType">Type of the SyncVar field.</param>
        /// <param name="fieldName">Name of the SyncVar field.</param>
        /// <param name="value">Value of the SyncVar.</param>
        /// <param name="clientMessage">Determines if this is a client message.</param>
        /// <returns>SyncVar message bytes if the fieldType is valid, null otherwise.</returns>
        public static byte[] createSyncVarMessage(NetworkBehaviour networkBehaviour, Type objType, Type fieldType, string fieldName, object value, bool clientMessage)
        {
            if (fieldType.IsSerializable)
            {
                return(MessageFactory.createBaseSyncVarMessage(networkBehaviour, objType, fieldType, fieldName, value));
            }
            else if (fieldType == typeof(GameObject))
            {
                return(MessageFactory.createGameObjectSyncVarMessage(networkBehaviour, objType, fieldName, (GameObject)value));
            }
            else if (fieldType == typeof(Vector3))
            {
                return(MessageFactory.createVector3SyncVarMessage(networkBehaviour, objType, fieldName, (Vector3)value));
            }
            else if (fieldType == typeof(Vector2))
            {
                return(MessageFactory.createVector2SyncVarMessage(networkBehaviour, objType, fieldName, (Vector2)value));
            }
            else if (fieldType == typeof(Quaternion))
            {
                return(MessageFactory.createQuaternionSyncVarMessage(networkBehaviour, objType, fieldName, (Quaternion)value));
            }
            else if (fieldType == typeof(Color))
            {
                return(MessageFactory.createColorSyncVarMessage(networkBehaviour, objType, fieldName, (Color)value));
            }

            return(null);
        }
コード例 #2
0
        private static void processGameObjectSyncVarMessage(byte[] msgBytes, bool clientMessage)
        {
            if (msgBytes != null && msgBytes.Length > 0)
            {
                short networkID = ObjectSerializer.deserializeShort(ref msgBytes);

                //Decompress the bytes which were compressed
                byte[] decompressedBytes = ObjectSerializer.deserializeBytes(ref msgBytes).decompress();

                System.Type objType   = ObjectSerializer.deserializeObject <System.Type>(ref decompressedBytes);
                string      fieldName = ObjectSerializer.deserializeString(ref decompressedBytes);

                if (NetworkEntityManager.Instance.findComponent(networkID, out NetworkIdentity netIdentity))
                {
                    if (netIdentity.getNetworkBehaviour(objType, out NetworkBehaviour netBehaviour))
                    {
                        if (NetworkEntityManager.Instance.search(ObjectSerializer.deserializeShort(ref msgBytes), out GameObject value))
                        {
                            netBehaviour.setSyncVar(objType, fieldName, value);

                            //Message was sent from a client, now being processed on the server
                            if (clientMessage && ServerBehaviour.Instance)
                            {
                                short  clientID             = ObjectSerializer.deserializeShort(ref msgBytes); //Client who sent the message
                                byte[] gameObjectSyncVarMsg = MessageFactory.createGameObjectSyncVarMessage(netBehaviour, objType, fieldName, value);
                                ServerBehaviour.Instance.sendMessage(gameObjectSyncVarMsg, clientID);          //Forward to all clients but the sender
                            }
                        }
                    }
                }
            }
        }