Tutorial:
This might be the simplest way to communicate between Arduino and Unity. I use the serial port to communicate between Arduino and Unity.
In Unity, set the network configuration as NET 2.0 instead of NET 2.0 subset.
Edit>ProjectSettings>Player>ApiCompatibilityLevel.
Arduino Part
- Set a baud rate in the Arduino code by Serial.begin(9600);
- Serial.flush(); Waits for the transmission of outgoing serial data to complete.
- Serial.println(); Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’). This command takes the same forms as Serial.print().
- The Port name can be found on the bottom right corner of the Arduino interface or on the Tools> Port > …. Mine is “/dev/cu.usmodem1421”
Unity
- Use the System.IO.Ports library
- Set the port and baud rate by SerialPort stream = new SerialPort (“device name here”, 9600);
- Initialize: stream.Open();
- In update, read the stream data by stream.ReadLine();
- Display the information received on GUI