/// <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); }
private static void processVector2SyncVarMessage(byte[] msgBytes, bool clientMessage) { 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)) { Vector2 value = new Vector2(); value.x = ObjectSerializer.deserializeFloat(ref msgBytes); value.y = ObjectSerializer.deserializeFloat(ref msgBytes); 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[] v2SyncVarMsg = MessageFactory.createVector2SyncVarMessage(netBehaviour, objType, fieldName, value); ServerBehaviour.Instance.sendMessage(v2SyncVarMsg, clientID); //Forward to all clients but the sender } } } }