Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Last active January 30, 2019 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olokobayusuf/dfb061b6b4097ac7ff000f78fd40beee to your computer and use it in GitHub Desktop.
Save olokobayusuf/dfb061b6b4097ac7ff000f78fd40beee to your computer and use it in GitHub Desktop.
A demo illustrating using the NatCam webcam and NatCorder video recording API's for Unity Engine.
using UnityEngine;
using UnityEngine.UI;
using NatCamU.Core;
using NatCorderU.Core;
using NatCorderU.Core.Clocks;
public class NatCamCorder : MonoBehaviour {
public RawImage rawImage;
private IClock recordingClock;
void Start () {
NatCam.StartPreview(DeviceCamera.RearCamera, OnStart, OnFrame);
}
void Update () {
// Toggle recording on a double-tap
foreach (var touch in Input.touches)
if (touch.phase == TouchPhase.Ended && touch.tapCount == 2)
if (!NatCorder.IsRecording) {
recordingClock = new RealtimeClock();
NatCorder.StartRecording(
Container.MP4,
new VideoFormat(NatCam.Preview.width, NatCam.Preview.height),
AudioFormat.None,
OnVideo
);
} else {
NatCorder.StopRecording();
}
}
#region --Callbacks--
void OnStart () {
// Display the camera preview
rawImage.texture = NatCam.Preview;
}
void OnFrame () {
if (NatCorder.IsRecording) {
var frame = NatCorder.AcquireFrame();
Graphics.Blit(NatCam.Preview, frame);
NatCorder.CommitFrame(frame, clock.CurrentTimestamp);
}
}
void OnVideo (string path) {
Handheld.PlayFullScreenMovie(path);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment