Getting Started
Up and Running in No Time
To begin, import the API with the Unity Package Manager by adding the following lines to your
Packages/manifest.json
file:{
"scopedRegistries": [
{
"name": "NatML",
"url": "https://registry.npmjs.com",
"scopes": ["ai.natml"]
}
],
"dependencies": {
"ai.natml.natdevice": "1.3.4",
...
}
}
In order to use NatDevice, you need a NatML access key, along with an active Cloud Plan subscription. Retrieve your access key from NatML Hub:
https://hub.natml.ai/profile
NatML Hub.
Once you have your access key, add it in
Project Settings > NatML
:
Specifying your NatML access key in Project Settings.
Using NatDevice requires an active NatML MediaKit subscription. You can try it out for free, but functionality is limited. See the FAQ for more info.
Now that we're all setup, let's write a minimal example to display the camera preview.
HelloCam.cs
using UnityEngine;
using UnityEngine.UI;
using NatML.Devices;
using NatML.Devices.Outputs;
public class HelloCam : MonoBehaviour {
[Header(@"UI")]
public RawImage rawImage;
public AspectRatioFitter aspectFitter;
async void Start () {
// Create a device query for the front camera
var filter = MediaDeviceCriteria.FrontCamera
var query = new MediaDeviceQuery(filter);
// Get the camera device
var device = query.current as CameraDevice;
// Start the camera preview
var textureOutput = new TextureOutput(); // stick around for an explainer
device.StartRunning(textureOutput);
// Display the preview in our UI
var previewTexture = await textureOutput;
rawImage.texture = previewTexture;
aspectFitter.aspectRatio = (float)previewTexture.width / previewTexture.height;
}
}
Now, let's setup our UI. We'll use a
RawImage
to display the camera preview to the screen. We will also add an AspectRatioFitter
so that the preview doesn't look stretched when it is displayed.
Now, let's setup
HelloCam
in our scene. We'll create an empty game object and add our script. Then, assign the RawImage
and AspectRatioFitter
we created above.
Now press Play and you should see a fine human smiling at you!
Last modified 5mo ago