Setting up OpenCV for Unity

Unity provides a cross-platform framework for scripting games in high-level languages such as C#. However, it also supports platform-specific plugins in languages such as C, C++, Objective-C (for Mac and iOS), and Java (for Android). Developers can publish these plugins (and other assets) on the Unity Asset Store. Many published plugins represent a large amount of high-quality work, and buying one might be more economical than writing your own.

OpenCV for Unity, by Enox Software (https://github.com/EnoxSoftware/), is a $75 plugin at the time of writing. It offers a C# API that is closely based on OpenCV's official Java (Android) bindings. However, the plugin wraps OpenCV's C++ libraries and is compatible with Android, iOS, Windows, and Mac. It is reliable, in my experience, and it saves us a lot of work that we would otherwise put into custom C++ code and C# wrappers. Moreover, it comes with several valuable samples.

Let's go shopping. Open Unity and create a new project, as described in Chapter 1, Preparing for the Mission, of this book. From the menu bar, select Asset Store under Window. If you have not already created a Unity account, follow the prompts to create one. Once you log into the store, you should see the Asset Store window. Enter OpenCV for Unity in the search bar in the upper-right corner.

Click on the OpenCV for Unity link among the search results. You should see something similar to the following screenshot:

Setting up OpenCV for Unity

Click on the Buy button and complete the transaction as directed. Click on the Download button and wait for the download to complete. Click on the Import button. You should now see the Importing package window, as shown in the next screenshot:

Setting up OpenCV for Unity

This is a list of all the files in the bundle that we just purchased. Ensure that all their checkboxes are checked, and then click on the Import button. Soon, you should see all the files in the Project pane of the Unity Editor.

The bundle includes further setup instructions and helpful links in the OpenCVForUnity/ReadMe.pdf file. Read the ReadMe! To run Rollingball on Android, we need to copy libopencvforunity.so and opencvforunity.jar from OpenCVForUnity/Plugins/Android to Plugins/Android. Also, to run one of OpenCV for Unity's samples, we must copy assets/haarcascade_frontalface_alt.xml from AOpenCVForUnity/Plugins/Android to Assets/Plugins/Android. Refer to the ReadMe's similar instructions for iOS if you wish to build for that platform.

Next let's try the samples.