Last active
January 30, 2019 13:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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