Products for USB Sensing and Control
Products for USB Sensing and Control

Unity and Phidgets - macOS

Learn how to add Phidgets to your Unity applications!


by Phidgets Education

Introduction

Now that you've completed the Getting Started Kit and have a basic understanding of programming with Phidgets, you can easily integrate sensors into your Unity projects.

Prerequisites

This project assumes you have an understanding of Unity and have completed the Getting Started Kit.

For more information, check out the Student Page.


Hardware

You will need:

  • A Getting Started Kit

Setup:

Connect your Getting Started Kit to your computer.


Add the Phidgets Library

In order to use Phidgets with Unity, you will need a copy of Phidget22.NET.dll. You can find different versions here in the Phidget22 Windows Development package.

The different versions are organized by date. Try to select the version that matches the libraries installed on your computer.


Start by creating a new 3D project named Phidgets_Unity_Test at the location of your choice.


Navigate to your project and right-click in the Assets window to select Import New Asset:


Find the folder you just downloaded and navigate to /phidget22-windevel/lib/dotnet/net46 and import Phidget22.NET.dll:


You've now successfully added the libraries to your project. The next step is creating your environment.

Note: if you are using an older version of Unity, check here for information on how to add the libraries.


Create Your Environment


Start by creating a 3D plane.


Next, add a sphere.


Adjust the sphere's Y-position to 0.5 so it sits on top of the plane.


Add a Rigidbody component to the sphere.


Next, add a script to the sphere called BallScript.


Finally, add some color to the plane to differentiate the objects.


Writing Code


The first step is to open your BallScript file and add a reference to Phidget22.

Note: If you get an error at this point, make sure you have added the Phidgets library properly (shown above).


Next, create a DigitalInput object for your button, as well as a Rigidbody component for your sphere.


Add a StateChange event for your button like you've done in previous projects. Also add a boolean called buttonState to track whether the button has been pressed or not.


Next, modify your Update method so the ball jumps when the button is pressed.


Finally, override the OnApplicationQuit method and close your Phidget object.


Try running your program. You should see your sphere jump when the button is pushed.


Conclusion

Now that you know how to use Phidgets with Unity, try creating your own project with the Phidgets in your classroom. If you need inspiration, check out the links below: