In this post I explain how to install an MQTT library in Unity with SSL enabled.
In my current job, I’m using Unity to show the datas we are gathering from probes, I’m using MQTT as communication, in the first version, I didn’t use any security but in this iteration I have to secure the communication with SSL and username and password.
I found a library named M2MQTT, unfortunately this library was not working with the Mono version provided with Unity.
I found a way to use this library with some limitation, only the TLSv1 is usable and I’m not validating the certificate.
So here is what I had to do to get it working on Unity v.2017.1.0f3, I’m on MacOS Sierra 10.12.6.
Clone the git repo
Open the project
M2Mqtt.sln with Visual Studio Community or Visual Studio Code.
Define in the target platform of the
.NET framework 4.0 as in this picture:
If you build the project you’ll have compilation error on the line 444 and 446 of the file
Just comment out the line and rebuild
Depending the configuration of the Compiler (Debug or Release) you’ll found the library
To import the DLL into Unity, just Drag and drop the
M2Mqtt.Net.dll into the
Assets folder in the
Project tab of Unity.
If you click on the DLL and look at the inspector, it should looks like :
It’s time to use MQTT in Unity, to do so, create a new Empty GameObject
GameObject -> CreateEmpty
Named it for example
On this GameObject, create a new component to add a new C# Script named for eg
In this script use the following properties:
Now, the inspector of the MQTT component should looks like:
And the initialization should be something like:
The connect method:
As I do not want to validate the certificate, I’m using this certification method:
The receive method:
And finally to publish message:
If you run the project, in the console you should have: