Unity + Arduino (MACOS) without Unity library

I wanted to make a simple example to connect Arduino to Unity. I used serial communication to talk to Unity from Arduino.

Unity version : 2018.3.1f1

In the Arduino side, I connected a touch sensor to Digital pin2 and referenced the code from ‘StatechangeDetection.ino’ in Arduino examples. (Only send a data when it changed) After sending “1”, flush the serial data.

In Unity side, I created a cube and add a c# script called ‘ArduinoSerial.cs’.

In ArduinoSerial.cs script,

  • add a line using System.IO.Ports;
  • match to Serial port info
  • print out the incoming data

For using ‘System.IO.Ports’, go to build setting / player setting / configuration tab. In the ‘Api compatibility level* needs to be .NET 4.x

In my experience, sometimes it didn’t apply right after changing. Save the scene and reboot the Unity.

There are some Arduino – Unity libraries in the Unity Asset store. but some of them are not free and some are designed for the PC environment. Check the spec before purchasing them if you need.

Leave a Reply

Your email address will not be published. Required fields are marked *