The Unity Toolkit for Intel® RealSense™ technology is a standard Unity package. It contains all the DLLs and scripts that are needed to use the Intel RealSense SDK in a Unity game or application. The toolkit allows for ease of use by making scripting properties available in the Inspector without the need to do much of the boilerplate coding required in the previous version of the SDK, the Intel® Perceptual Computing SDK.
Purpose
The purpose of this tutorial is to show you how to use the Unity Toolkit’s SendMessage action. The SendMessage action allows you to call a function from another script whenever an Intel RealSense application event is triggered. This can be very useful when you want to use functionality from a different script than what is included in any of the action scripts.
What this Tutorial Covers
This tutorial will cover the steps needed to use the SendMessageAction action.
Assumptions
You must have read the sdktoolkit.pdf file, which is installed in the \RSSDK\doc folder. This document gives you a general idea how the Unity add-on works. This tutorial also assumes that you have some basic Unity skills such as importing an external package, adding a cube onto a scene, and applying a script to a game object.
Requirements
Unity Professional 4.1 or higher.
An Intel® RealSense™ 3D camera either embedded in a device or an external camera.
Let’s Begin
Import the Visual Studio* Plugin (Optional)
While this step is not required, I prefer to use Visual Studio as my IDE. If you would like to use this free plugin, you can download it here http://unityvs.com/.
Import the Unity* package for Intel® RealSense™ Technology
This package contains everything you need to set up and run Intel RealSense applications. The Unity package is located in the RSSDK\Framework\Unity folder. If you installed the Intel RealSense SDK in the default location, the RSSDK folder is in C:\Program Files (x86).
You can import the Unity Toolkit as you would any package. When doing so, you have the options to pick and choose what components you want to use. For the purpose of this tutorial, we will use the defaults and import everything.
As you can see in the following image, there are now several new items under the Assets folder.
- Plugins and Plugins.Managed contain DLLs required for using the Intel RealSense SDK.
- RSUnityToolkit folder contains all the scripts and assets for running the toolkit.
We won’t go into what all the folders are and what they contain here as that is out of the scope of this tutorial.
Game Objects
Add a 3D cube to the scene.
Add a Directional light to give the ship some light so you can see your game object better.
Scripting
To make sure the SendMessageAction action works properly on a game object, you first need to drag and drop the SendMessageAction script onto the game object. Second, you need to create a new script that will contain a function that you want to be called whenever an Intel RealSense SDK action has been triggered.
Create a new folder, then create a new script
To keep things organized, I created a new folder to hold my custom script. Inside that folder, I created a new C# script called MyRotateScript. Inside this script I set up a function that will rotate the cube along the X axis at a set interval.
Open the script for editing
Clear out any existing functions as they won’t be needed. Your script should look like this:
Next add the following code to your script:
Any time this function is called, the cube is rotated 30 degrees on each axis.
Once you have saved your script, return to Unity and drag and drop this new script onto your cube game object. You won’t need to worry about editing this script in the Inspector. However, it is important that you add this script to your cube game object. The SendMessageScript relies on the fact that this script is attached to the same game object.
Add the SendMessageAction script
Grab the SendMessageAction script and drop it onto the cube.
At this point in the Inspector you should see the two scripts, the MyRotateScript we just created and the SendMessageAction script.
Configure the Send Message Action script to call the rotation scripts RotateMyCube()
On the Add Trigger button, select Event Source.
On the Event Source Add button, I have chosen Hand Detected. This is probably the easiest way to get this up and working.
Next choose the hand options. I have chosen Right hand. What this will do is that any time the camera sees my hand it will enable the Send Message Action.
Under Function Name enter the name of the public function RotateMyCube, which resides in the MyRotateScript class that was created earlier.
One thing to note, the event fires only when the SDK detects your hand, and only one message is sent at a time. So, to see the cube rotate more than once, you must move your hand out of the camera’s view and bring it back.
Congratulations, at this point, everything is ready to go.
Save your scene,
Save your project,
and run the application you just created!