Tuesday, July 22, 2014

Unity 3D and Phidgets: Basic Setup

For those of you out there interested in using Unity for physical computing, I'd like to let you in on a fantastic hardware company: Phidgets. They make everything from LCD displays to motor controllers to I/O boards and more. I've used nearly every type of product they make in products that total thousands of unit shipped, and I've found them to be simple, robust, reliable, and an all-around joy to work with.

This basic setup will get you working with any devices that Phidgets make. There are a few little gotchas in the setup, but once you get to the point where Unity recognizes them, they're quite simple to work with. This article will deal with the universal setup for any Phidgets device, and then I'm going to work up a series on the specifics of each type of device.

We're going to assume some working knowledge of Unity (navigating the various panes, creating objects, etc.) and C#, so there won't be as much explanation on basic topics as usual. If you're not at that point yet, I recommend you check out the Unity Basics, and the BurgZerg Arcade Hack & Slash Tutorial. BurgZerg's is my favorite for going from basic Unity knowledge to working game, learning some very good C# along the way.

Here are the steps we're going to take to get Phidgets working in Unity on a Windows machine:

1. Download and run the Phidgets Installer. This will be required for basic communication with Phidgets devices. Make sure that you get the correct installer for your OS (32-bit or 64-bit). If you're not sure which one you have, you can right-click on Computer from the Start Menu, select "Properties", and look under the System heading. Under "System type", you will see that you have either a 32-bit or 64-bit operating system. If you're going to develop on 64-bit Windows but your production machines will be 32-bit, you'll want the 64-bit installer on your development machine and the 32-bit installer for your production machines.


2. Plug in your device and make sure it is recognized. Whichever Phidget you have, go ahead and connect it, then open the Phidget Control Panel. Do so by opening the taskbar in the lower-right-hand corner of the screen, and double-clicking the little "Ph" icon.


In the Control Panel, you should see an entry for each attached device, as well as the serial and version numbers. If you double-click on a device entry, it will open the test program for that device. Once you're satisfied that your device is recognized and functional, you can close the Control Panel and move on.


3. If you haven't already, create a new Unity project.

4. Copy the Phidget21.NET.dll file to your Assets folder. For the sake of organization, I prefer to make a folder under Assets called "DLLs", and move it to there. If you installed to the default directory, this DLL will be found in either "C:\Program Files\Phidgets" or "C:\Program Files(x86)\Phidgets".

5. Create a C# script to control your Phidgets device. It's really up to you how you do this; I prefer to create a separate script for each type of device. At the top your script, you're going to need to add the following lines of code:

using Phidgets;
using Phidgets.Events;


These lines will allow you to talk to your Phidgets devices and utilize their event system.

That's pretty much it! Now, I have had some issues getting Phidgets to work initially, although once they work, they've always continued to work. Occasionally, I've had to restart after adding the DLL to the Assets folder before any communication would take place. Also, in older versions of Unity, I had to change from the .NET 2.0 subset to .NET 2.0 in Player Settings, but lately I haven't had to. Finally, I've read that some folks needed to add a reference to the Phidgets DLL in MonoDevelop, but personally, I've never had to.

So you can use this method to talk to any Phidgets devices. I'll be tackling some of them in a future series, and this will serve as the basis for all of those tutorials. In the meantime, head over to their site and get your imagination working!

No comments:

Post a Comment