Unity Native Plugin for Asus Xtion2

Unity is one of the main game engines currently being used by many companies but it is also good for other types of interactive entertainment too. During one of my recent projects, I needed to integrate the Asus Xtion2 Camera with Unity to get its depth values. Since the Xtion2 SDK does not support Unity out of the box I needed to write my own native plugin.

Test Environment

  • Windows 10 64 bit.
  • Unity 2017.2.0f3 x64. Important! Choose x64 or x86 to match your Unity installation.
  • OpenNI For Xtion2 SDK. The official SDK is somewhat different from the OpenNI SDK provided by Asus but it should behave the same. The one provided by Asus can be downloaded here. Make sure you choose the latest one.
  • CMake 3.0 or higher

Who is it for

Someone who has been using Unity for some time and is comfortable with the concept of classes and objects. It will be very helpful if you know C++ and pointers too.

Steps

Since the code is provided, I will only go over the major steps. Let me know in the comments if I miss anything.

  1. Build and install OpenNI. Since there are already many tutorials on building and installing libraries I will not go into the details here.
  2. Assuming OpenNI is installed in {OPENNI_INSTALL_PATH}, make sure that you have {OPENNI_INSTALL_PATH}/lib/OpenNI2.lib and you can also run the samples located in {OPENNI_INSTALL_PATH}/Samples/Bin
  3. You can find the code for the native plugin here.
  4. The Unity project can be found here.
  5. Both lack documentation so in the meantime please check out the code and let me know in the comments if anything is unclear.
  6. You can find the CMakeList file for the native code under src/ folder.
  7. Adjust the install paths and parameters to suit your environment and build with CMake.
  8. If everything goes well with CMake you should see a solution (.sln) file in the build folder
  9. Open this solution file with Visual Studio and build the project. Don't forget to set the build settings to either Debug or Release and make sure to build x86 or x64 according to your architecture.
  10. If the build is successful, you should see an XtionCapture.dll in the build folder.
  11. Copy this dll to the Unity project in Assets/Plugins.
  12. You will also need to copy some file from the OpenNI SDK. Go to {OPENNI_INSTALL_PATH}/Redist and copy all the files there into the Assets/Plugins folder.
  13. If you managed to come this far then just press Play and enjoy!

Overview of plugin interface

The details can be found in the code but I'd like to share the idea behind the plugin.

  1. Instantiate a new capture class (xtion_capture) and let this class hold references to the Xtion Device. So pointers to device handlers, depth frames etc. are registered as this class' members.
  2. When xtion_capture is instantiated it returns a pointer to its object instance to Unity. This pointer is how Unity calls the capture class and retrieves data from its (member) pointers. The above design is illustrated below.

    Future work

    I will definitely want to add documentations, and clean up the repository. If I have the time I'd try getting the color image too. Best of luck and I would be happy to help of anyone needs to have the same system setup.

AUTHOR

Thank you for your first article. I'm looking forward to sharing your knowledge.

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Bitcoin Gambling

Delete an article

Deleted articles are gone forever. Are you sure?