NatCam Tutorial Series 1: Starting Off

Welcome to the NatCam Tutorial Series. We decided to move to written tutorials as they are much easier to follow, and easier to update given the rate at which we make changes to the API. Just in case you wandered here from the wild, I’ll explain what NatCam is and how it came to be. Get the popcorn ready!

NatCam was started in the Fall of 2015. I was working at a startup that needed to scan barcodes in Unity. There was no solution at the time, and the closest (WebCamTexture) was simply unsatisfactory. So I decided to create an API that would do just what we needed of it. Fast-forward one and a half years later, we’re here!

So just what does NatCam do? Glad you asked. Here’s a (non-exhaustive) list:

  • Fluid camera preview, much unlike WebCamTexture
  • High resolution previews (1920x1080 and higher on some devices)
  • Custom resolution previews, as high or low as you need
  • Photo capture. Take high-res stills at 8MP, 12MP, or higher
  • Flash. When capturing photos, set the flash mode
  • Focus, and autofocus
  • Exposure control. Brighten or darken the scene
  • Zoom, optically by the camera
  • Torch. Put on the torch, regardless of whether the camera preview is running
  • VR Support, for all our Cardboard and GearVR enthusiasts out there
  • Preview data. Get access to the preview data as it is being streamed
  • Sources. Get access to the native sources*

Starting Off: Key Concepts

Before we delve into code, I’d like to explain a few key concepts you will encounter severally when working with NatCam:

  • Camera: NatCam provides access to the hardware cameras on a device using the DeviceCamera class. This is different from a Unity camera, which renders the game view. With the this class, you can do things like setting resolutions, framerate, exposure, focus, flash, and so on.
  • Preview: This is the camera preview. When you start a camera (using NatCam.Play), a stream of data will begin from the camera. This stream will contain the preview (or more specifically, the preview frames that constitute the preview). You are able to configure the size (resolution) of the preview by requesting it with DeviceCamera.SetPreviewResolution. NatCam provides this stream in a Texture object, NatCam.Preview.

Enough Talk, Let’s Code

I suggest starting with a blank project in Unity. Import NatCam (Core or Pro) and follow along. If you have questions, make sure to ask them in the comments of this post. Now, let’s begin.

Our goal is to start the rear camera preview and display it to the screen. First, we create a fullscreen RawImage where we will display the preview texture:

Make the RawImage cover the entire screen

Next, let us create a C# script. I’ll call mine StartingOff:

Here’s an explanation of what’s going on:

  1. To start the camera preview, we simply call NatCam.Play with the camera we want to play from.
  2. We subscribe a delegate to the NatCam.OnStart event. This event is broadcast by NatCam when the camera preview actually starts because it is an asynchronous process.
  3. In our OnStart delegate, we display the NatCam.Preview texture on our RawImage. Why didn’t we simply do this in Start? Well, the preview texture only becomes available when the camera preview has actually started. So prior to the OnStart event being called, NatCam.Preview will be null.
Now build and run!

Now, you might notice some stretching in the camera preview. This stretching is as a result of the difference between the aspect ratios of the camera preview and RawImage used to display it. To eliminate stretching, both aspect ratios must be the same. We will use the AspectRatioFitter component that comes with Unity to set the aspect ratio:

Add the AspectRatioFitter component onto the RawImage then add a reference to it in the StartingOff component (as shown below):


That’s it for today folks! In the next tutorial, I will explore the NatCam Control Pipeline, showing how to perform different camera operations using the DeviceCamera class.

— Lanre Olokoba.


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

Like what you read? Give Lanre Olokoba a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.