Getting Started

Up and Running in No Time

Importing NatCorder

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.natcorder": "1.9.4",
    ...
  }
}

Specifying your Access Key

In order to use NatCorder, you need a NatML access key. Retrieve your access key from NatML Hub:

Once you have your access key, add it in Project Settings > NatML:

Using NatCorder requires an active NatML MediaKit subscription. You can try it out for free, but some functionality is limited. See the FAQ for more info.

Recording the Camera

Once the API is imported, you can begin to write recording code. Let's create a recorder to record a WebCamTexture.

RecordWebCam.cs
using System.Threading.Tasks;
using UnityEngine;
using NatML.Recorders;
using NatML.Recorders.Clocks;

public class RecordWebCam : MonoBehaviour {
    
    async void Start () {
        // Start the webcam
        var cameraTexture = new WebCamTexture();
        cameraTexture.Play();
        // Wait for user to smile :)
        await Task.Delay(2000);
        // Create a recorder
        var recorder = new MP4Recorder(cameraTexture.width, cameraTexture.height, 30);
        // Create a recording clock to generate timestamps
        var clock = new RealtimeClock();
        // Record for a few seconds
        for (var i = 0; i < 300; i++) {
            // Commit frame
            recorder.CommitFrame(cameraTexture.GetPixels32(), clock.timestamp);            
            // Wait till next frame
            await Task.Yield();
        }
        // All done
        var path = await recorder.FinishWriting();
        Debug.Log($"Recorded video to {path}");
    }
}

Add this component to any game object in your scene and enter Play mode. After about 5 seconds, you should find a .mp4 file sitting in your project folder!

Before going into more details, look through the minimum requirements for developing with and deploying NatCorder.

Last updated