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.

3 Comments

  1. Jamo

    THX A BUNCH!

    YOU MIGHT HAVE SAVED MY PROJECT!

    ALONG THE SEA OF PEOPLE SAYING “DOWNLOAD A DIFFERENT LIBRARY” OR “SWITCH NET 2.0 SUBSET TO NET 2.0”, YOU WERE THE ONE TO STAND UP FOR THE LITTLE GUY, THE ONE WITH THE NEWEST VERSION OF UNITY AND NEXT TO ZERO KNOWLEDGE OF EITHER UNITY OR ARDUINO.

    no seriously, thanks a bunch. After 2 hours of searching and coding, to no avail, your explanation might be my first coding succes that made me scream from joy. Who could have known that transferring a simple buttonpush could be so hard?

    • hhayley

      I am glad that this blog was helpful for your project. Actually, I try to make a few examples of Arduino and Unity. Let me know if you find more issues or questions. 🙂

  2. Pingback: [Unity + Arduino] Analog input from Arduino – Hayley.H

Leave a Reply

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