USB Base Custom Hardware Interface for Unity3D

This is a simple project to demonstrate the USB custom hardware interfacing with Unity3D game engine on top of Microsoft Windows operating system(s).

The custom hardware unit used in this demo is build around Microchip’s PIC18F2550 MCU. This custom USB controller consists of 4 push buttons and a linear potentiometer. In the supplied demo, the user needs to control the aircraft with those buttons and the potentiometer. According to the game logic 4 buttons are used to control the flying direction and flying angle of the aircraft and the potentiometer is used to control the speed of the aircraft.

As illustrated in figure above, the host environment consists of 2 main applications such as Session Controller and Unity3D game. Session Controller is responsible for USB communication and data conversions. It’s a native application written using Delphi and it gets started with Unity game project. Communication between Session controller and Unity game project is happening through an OS level shared memory location. In this demo both Session Controller and Unity game project are heavily dependent on Windows API functions, and also both the applications require administrative privileges to execute.

In this demo project MCU firmware is developed using MikroC PRO 5.0. Session controller is developed using Embarcadero Delphi XE3 and all the Unity scripts are in C#. HID interface of this project is based around J.W. Beunder’s Delphi HID library.

The microcontroller firmware consists of a simple port scanner and ADC (Analog to Digital Converter) scanner. When the scanner dectects some change in input, it transmits all the "port values" and "ADC value" to the USB HID buffer.

Microcontroller firmware is listed below and it is specially designed for PIC18F2550 MCU, but it can be used with PIC18F2455, PIC18F4455 and PIC18F4550 MCUs with slight modifications.

As described earlier, interface between the game and USB HID peripheral is made using a Delphi application. This application creates named shared memory and writes all the processed data to that space. Because of this techinique multiple game instances can read the USB controller's data and it also reduces the synchronization issues between the game and hardware device. This interface code is listed below:

In Unity, the above mentioned shared memory is accessed using the same Windows API functions and its implementation is available in the UHWComLink.cs file. GetHIDControlData is the function to get all the shared memory data and it's listed below:

A schematic of the USB game controller is illustrated in the next figure. This can be constructed using breadboard, stripboard or PCB (Printed Circuit Board). Recommended platform to build this controller is a PCB and the complete PCB design pattern is available at the project repository.

The supplied PCB design of this project is based on commonly-available SMD components. Please note that this hardware setup is quite sensitive to external noises, so it is recommended to use some properly-grounded shield with this controller. If the USB connection between the host and the controller is more than 1.5m, it is advisable to use a USB cable with ferrite bead(s).

All the source code and design documents of this project are available to download at github.com/dilshan/unityusb. A demonstration video of the prototyped system can be viewed here

License

It should be noted that nothing about the hardware ties this to Unity or Windows.

It's purposely done to avoid any hard dependencies with other OSs and game engines. By referring this article anyone can use this hardware piece with other operating systems like Linux and game developmenent kits like UDK, irrlicht, etc.

But in this article we focuse Windows and Unity because both of these technologies are quiet familiar to many developers and easy to understand / explain to any level of developer.

Meantime I wants to know if there are any Unity inbuild custom hardware interface is exists ???

My point in noting that was just so that it would be noted. There may be those interested that wouldn't know this. It wasn't a criticism.

If you are going to vote down a post, make sure it is abusive, malicious or completely off topic. Down voting a post because you simply disagree with what was said, especially if it was correct, is simply cheap.

I wasn't being critical. However, if you cannot accept criticism on your article, don't write the article.

My point in noting that was just so that it would be noted. There may be those interested that wouldn't know this. It wasn't a criticism.

If you are going to vote down a post, make sure it is abusive, malicious or completely off topic. Down voting a post because you simply disagree with what was said, especially if it was correct, is simply cheap.

I wasn't being critical. However, if you cannot accept criticism on your article, don't write the article.

I always accepting criticisim BUT it need to be subjective and point-blank. If you carefully observe the code listsings you never make your initial comment. So in my context I treat your initial comment as "vague & careless" one.