NatCam Tutorial Series X: Goodies

Lanre Olokoba
Jul 30, 2017 · 2 min read

Hello guys, welcome to a different form of NatCam tutorial. In this tutorial, we won’t be going over camera functionality (per se), but will be focusing on some useful utilities included in NatCam that you may find useful for other projects. You don’t need to, but I recommend checking out the previous tutorials: part 1, part 2, and part 3. Let’s get started

Threading Made Simple: IDispatch

When doing interop with the native layer, we often found ourselves having to queue certain managed callbacks (invoked from the native layer) on the main thread. This is so because all Unity API’s (except Debug) are not thread safe, so Unity will scream at you if you tried to make an API call from a non-Unity/main thread. In order to abstract the headache of queueing delegates on the main thread, and further queueing delegates to be invoked on background threads, we created the IDispatch module.

The task is simple: given a workload, we want to be able to hand it to something — a black box of sortsso that the workload will be executed on some thread. That something is an implementation of IDispatch. IDispatch was modeled after Grand Central Dispatch dispatch queues. Enough talk, I’ll show you how to use it with a few examples.

Calculating Random Things

As always, we’ll start with a clean project and import NatCam. Then let’s create a new MonoBehaviour called Calculator.

Make sure the Profiler is open. Run the scene and press the space bar; the Editor should freeze for a few seconds:

It froze for about 5.8 seconds

Now let us dispatch this calculation to a worker thread using IDispatch:

Run it again and see the difference:

Now I should clarify that the dispatcher doesn’t magically make things faster. The workload still takes the same amount of time to complete, but it does so without stopping the main thread. This becomes especially useful for things like animating thousands of vertices while keeping a smooth framerate in your app.

Since an IDispatch instance holds onto resources (threads and so on), we must make sure to release it once we are done with it. Every IDispatch instance inherits from IDisposable, which lets us use it in a using block:


That’s it for now! In the next set of tutorials, we will be looking into the functionality in NatCam Pro, starting with video recording. See you soon.

— Lanre Olokoba.


NatCam is a cross-platform camera API for the Unity Engine. Get NatCam here: NatCam Core, NatCam Pro.

Lanre Olokoba

Written by

Trying to better understand life through philosophy, mathematics, and computer science. Proprietor @ NatSuite API's. Founding member @ Homedeck.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade