Skip to main content
Version: 1.1.0

Sending / Publishing Messages

All messages are sent through the central Publisher component. Therefore, no client ID is required on any of the following components.

SendOnEvent

This component can be attached to an object that is supposed to send an MQTT message upon an Eventsystem event (like Pointer Enter, Begin Drag, etc.) It covers the same Events as Unity's EventTrigger component. You can define a topic and a payload on the component.

tip

Just like with Unity Events, you need to setup a few things. There needs to be a collider on the object, an Eventsystem in the scene and a PhysicsRaycaster on the Camera.


PublishValue

PublishValue allows you to send data to the topic specified in this component. You can hook up the following methods to any UnityEvent (e.g. a slider's OnValueChange event, a Button's OnClick, or an EventTrigger):

  • OnPublishString
  • OnPublishFloat
  • OnPublishDouble
  • OnPublishInt
  • OnPublishBool The values passed are converted to string before publishing as MQTT message.

NOTE: Hooking up a slider will send an MQTT message everytime the slider changes (i.e. everytime the mouse moves while dragging). This creates a lot of messages and might affect performance if used too extensively.


Other Publish Components

For more complex values, the following components are available

PublishColor

This component will allow you to select a color on a color picker to be sent via MQTT. Colors will be converted to hexRGB (e.g. 100% red => FF0000). The default prefix is # however it can be changed to anything that is not 0-9 or A-F.

message format #RRGGBB (R=Red, G=Green, B=Blue)


PublishQuaternion

If you want to send rotations, use this component. You can select whether to publish the rotation of

  • a reference transform (global rotation)
  • quaternion values x, y, z, w
  • Euler angles x, y, z

Set the type accordingly (Reference Transform, Quaternion, Euler Angles). Note: Only the values for the selected type are considered. If for example you select Euler Angles, a linked Transform or values in the quaternion section in inspector will be ignored and the message will be build just from the vector3 (interpreted as Euler Angles). The message payload will always be a quaternion though.

message format (x, y, z, w)

PublishVector3

Vector3 MQTT messages can be used for positions, scale, and such. For the moment PublishVector3 will send the Vector3 configured in the inspector. See roadmap for future plans.


PublishFromTMPInputField

Sending data from an input field is a common use-case. In contrast to the other publish components, this one needs to be on the same gameobject as the according TMP_InputField. When triggered by button or eventtrigger (see picture below) it will send the text entered into the inputfield.


PublishTriggerCollision

In general, this component allows reporting of collisions or trigger events to MQTT when the gameobject enters or exits (see below for On...Stay event) another collider.

Prerequisites for trigger:

  • Both interacting objects need to have colliders
  • One of the colliders has to be a trigger

Prerequisites for collision:

  • Both interacting objects need to have colliders
  • In case of Mesh Colliders, it either needs to be convex or kinematic.

If it does not work for any reason, cross-check Unity Manual: Collision Matrix for detailed collision information

Inspector Config: MQTT As usual you can configure the topic on which a collision event will be published. Since it can be beneficial, I added some dynamic values (see table and inspector examples below) to be automatically replaced in the topic string. If you want to use these placeholders, make sure Format Topic String is checked. Finally select with Is Trigger, whether you want to react to trigger events (checked) or to collision events (unchecked).

Source and Target The Source Collider is supposed to be the main collider involved in an event. Typically this is the first collider of a gameobject and it will be automatically set to that, but if for some reason you want to use a different one, feel free to override it. The target settings allow you to filter collisions to only report on specific objects (Filter Target Game Object) or Tags (Filter Target Tag) e.g. "Player". If you set both filters, they are BOTH required (logical AND). Filtering by tag usually does not make sense, if you link the exact gameobject, unless you want to change the Filter Target GameObject at runtime (through code) and constrain events to e.g. Tag=Enemy.

Actions to Publish Here you can select, which Events you want to publish as MQTT message.

OnStay events If you want to publish a message while an object is within a trigger volume, you can check Emit MQTT On Stay. It is crucial, that you set an emit interval that is as long as possible for your use case. Without an interval it would send a message every frame (i.e. >>30 messages per second) and clutter up your broker. Therefore an interval is mandatory and should not be lower than 0.5 seconds. The unit of Emit Interval is seconds (between messages). Be careful with these settings, as they can easily kill your performance and/or fill your message queue and cause delays in other MQTT messages.

NOTE: "Collision stay events are not sent for sleeping Rigidbodies." Unity Manual

placeholder overview

nameplaceholderdescriptionpossible values
(always lower case)
name%nShows the name of the gameobject having the publish componentactual name e.g. "cube"
type%tShows whether it's a collision or a trigger event"collision" or "trigger"
action%aShows the kind of event"enter", "stay", or "exit"

message format The default message format is type "name" action "other.name", so a "Cube" object colliding with a "Plane" would send Collision "Cube" Enter "Plane" (Names are quoted in payload, because they can contain spaces)

The message format changes when using Format Topic String in a way that all information used in the topic string will be omitted from the payload.

Example 1: Upon colliding with "Plane" a "Cube" with format string "%n/%t" would send Enter "Plane" to topic cube/collision.

Example 2: Upon colliding with "Plane" a "Cube" with format string "%n/%t/%a" would just send "Plane" to topic cube/collision/enter.