Yoctopuce and Unity 5

Yoctopuce and Unity 5

Several customers asked us recently whether one could use our modules with the Unity game engine. As we had absolutely no experience with this environment, we decided to give it a try and to let you know the result...

Unity is a very popular game engine enabling you to create games for numerous platforms (Windows, OS X, Linux, but also iOS, Android, and so on). This engine is based on the Mono framework and you can write the game logic in C#. This is great, we have a C# library :-)

To check that you can use our library with Unity, we have created a small 2D project with a few text fields which are updated with the values of the sensors of a Yocto-Meteo. The "up" and "down" keys enable you to change the color of the leds of a Yocto-Color. This project, simple to the extreme, aims only at checking that our library behaves correctly with the Unity engine.

Code extract calling our library:

public class TestScript : MonoBehaviour

  public Text myText;


  YColorLed led1 = null, led2 = null;

  // Use this for initialization
  void Start ()

    string msg = "";
    if (YAPI.RegisterHub ("usb", ref msg) != YAPI.SUCCESS) {
      state = State.ERROR;
      errmsg = msg;
    YModule module = YModule.FirstModule ();
    while (module!=null) {
      string product = module.get_productName ();
      string serial = module.get_serialNumber ();
      if (product == "Yocto-Color") {
        colorSerial = serial;
        led1 = YColorLed.FindColorLed (serial + ".colorLed1");
        led2 = YColorLed.FindColorLed (serial + ".colorLed2");
      module = module.nextModule ();


  // Update is called once per frame
  void Update ()
    if (Input.GetKey (KeyCode.UpArrow)) {
      colorhue ++;
      if (colorhue > 0xff) {
        colorhue = 0;
      led1.set_hslColor ((colorhue << 16) + 0xff80);
      led2.set_hslColor ((colorhue << 16) + 0xff80);
    } else if (Input.GetKey (KeyCode.DownArrow)) {
      colorhue --;
      if (colorhue < 0) {
        colorhue = 0xff;
      led1.set_hslColor ((colorhue << 16) + 0xff80);
      led2.set_hslColor ((colorhue << 16) + 0xff80);
    } else if (Input.GetKeyDown (KeyCode.Escape)) {
      Application.Quit ();


Now that we wrote the code, we simply need to compile the project.

The C# Yoctopuce library

As a reminder, our C# library is composed of two parts: the C# files implementing the available classes (YModule, YTemperature, YRelay, etc.) and a dynamic library yapi managing the USB or TCP communication with the Yoctopuce modules.

Up to now, our C# library included only the Windows versions of this dynamic library (both the Windows 32bit and 64bit versions). Since the version 21486, the Library also contains the dynamic library yapi for Linux 32bit, Linux 64bit and OS X. All pre-compiled versions are now available in the /unity directory of the archive.

Compiling the Unity project

To get our library running, the .cs files must be compiled and the dynamic library yapi must be copied to the directory of the compiled executable. Compiling the .cs is trivial: you only need to copy the .cs files in the project Assets directory for them to be automatically compiled and available in the project.

For the the dynamic library yapi, it's a bit more complex: you need to use Plugins. Plugins are a mechanism that will let you to include in the build dynamic libraries used by the game. During the compilation, Unity copies the dynamics libraries found in the folder /assets/Plugings to the final directory of the game. You can even differentiate the 32-bit and 64-bit versions of a library by creating subfolders named /Assets/Plugings/x86 and /Assets/Plugings/x86_64.

Concretely, to be able to generate build for Windows, Linux and OX X, you must respect the following hierarchy:

  • /Assets/Plugins/x86/ contains libraries for Windows and Linux 32bit
  • /Assets/Plugins/x86_64/ contains libraries for Windows and Linux 64bit
  • /Assets/Plugins/ contains libraries for Mac OS X

We have used the same convention to store the different versions of the dynamic library in the Yoctopuce library archive. So you only need to copy the whole content of the /unity directory in the /Assets/Plugins/ folder to get a working Unity project.

To resume

To use the Yoctopuce C# Library, you need to:

  1. Copoy the files .cs of the directory /Source into the folder /Assets of the Unity project.
  2. Copy the content of the directory unity into the folder /Assets/plugins of the Unity project.

When the .cs files and all the dynamic libraries are added to the project, you can test the game directly from the editor, as well as generate builds for any OS.

If you want to start from a existing project you can use our project witch is available on GitHub.


Our library works fine with Unity. For this post, we used Unity 5, but we also checked that it works with Unity 4.6. There is one limitation though: due to the dynamic library architecture, iOS and Android are not supported.

The "game" that we wrote is clearly uninteresting because the aim of this project was to discover the environment and to test our library. But it's quite possible that we'll try to create something more "fun" for a following post...

Add a comment No comment yet
Back to blog

Yoctopuce, get your stuff connected.