Hello,
currently I’m working on a project that needs to record what the user is observing in the Oculus Quest 2. The recording should be triggered by an UI button that the user can use whenever it wants.
I didn’t find any dedicated asset for this purpose, but I found a solution for Android, in particular a screen recorder. It’s a simple asset that i tested in my smathphone at it’s working well (record the screen using a button from the UI, and save it to the gallery).
In the Oculus is the same but the videos are empty! I guess because I’m not recording anymore the “screen of the adroid device”. For example my the Start() method I have this code:
private void Start()
{
DontDestroyOnLoad(gameObject);
#if UNITY_ANDROID && !UNITY_EDITOR
using (AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
androidRecorder = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
int width = (int)(Screen.width > SCREEN_WIDTH ? SCREEN_WIDTH : Screen.width);
int height = Screen.width > SCREEN_WIDTH ? (int)(Screen.height * SCREEN_WIDTH / Screen.width) : Screen.height;
int bitrate = (int)(1f * width * height / 100 * 240 * 7);
int fps = 30;
bool audioEnable=true;
androidRecorder.Call("setupVideo", width, height,bitrate, fps, audioEnable);
androidRecorder.Call("setCallback","AndroidUtils","VideoRecorderCallback");
}
There is any documentation or guide that can help me to setup the video and record from The Oculus?
1 Like
Hi @Washi92 Did u find a solution? I’m looking for the same thing, except I need it for standalone windows and mac builds.
1 Like
If you plan on replaying the video as a 2D image, I have a simple solution for you:
// Attach to the camera that you want to record. The recording will start on enable. Disable to stop recording, enable to resume recording.
// If you use this code, consider limiting the recording length but using a queue, or automatically disabling the component. You don't want an out-of-memory error.
[RequireComponent(typeof(Camera))]
public class VideoCapture : MonoBehaviour
{
// We could create the render texture at runtime, but this gives you more control over the capture quality
public RenderTexture videoRenderTexture;
[Tooltip("Video image color format")]
public TextureFormat saveFormat = TextureFormat.RGB24;
private Camera _camera;
private Camera recordingCamera;
private List<Texture2D> imageSequence;
private void Start()
{
_camera = GetComponent<Camera>();
InitRecordingCamera();
imageSequence = new List<Texture2D>();
}
// Use fixed update to keep a constant framerate for playback.
private void FixedUpdate()
{
imageSequence.Add(RenderTextureToTexture2D(videoRenderTexture));
}
private void InitRecordingCamera()
{
if (recordingCamera != null)
Destroy(recordingCamera.gameObject);
// Create our own camera so that the fov, culling mask, etc. match the attached camera.
GameObject go = new GameObject("Recording Camera [" + _camera.name + "]");
go.transform.parent = transform;
recordingCamera = go.AddComponent<Camera>();
recordingCamera.CopyFrom(_camera);
recordingCamera.targetTexture = videoRenderTexture;
}
private Texture2D RenderTextureToTexture2D(RenderTexture renderTexture)
{
Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height, saveFormat, false);
RenderTexture.active = renderTexture;
tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
tex.Apply();
return tex;
}
public Texture2D[] GetImageSequence()
{
return imageSequence.ToArray();
}
}
// An example for playing back the recording. Attach to a world space canvas with a RawImage UI component.
[RequireComponent(typeof(RawImage))]
public class ImageSequenceViewer : MonoBehaviour
{
private RawImage _image;
private int index = -1;
private Texture2D[] sequence;
private void Start()
{
_image = GetComponent<RawImage>();
}
private void FixedUpdate()
{
if (index > -1 || index < sequence.Length)
{
_image.texture = sequence[index];
index++;
}
}
public void PlayImageSequence(Texture2D[] sequence)
{
this.sequence = sequence;
index = 0;
}
}
This is not an actual video file, but just a sequence of images. If you also want the functionality to save and play the video outside of unity, look into saving image sequences as video files. Note that this does not have any audio recorded with the ‘video’.
There are some comments, but if you need any help or were looking for something different, let me know.