UP-Viagg-io/Viagg-io/Assets/Scripts/Screenshot.cs

82 lines
2.4 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
2025-02-06 07:33:52 +01:00
using Oculus.Platform;
public class Screenshot : MonoBehaviour
{
[SerializeField]
RawImage _previewImg;
2025-02-06 07:33:52 +01:00
GameObject XRCamera; //the XR Player Camera, named "Main Camera"
GameObject XRHoloVideoCamera; //the XR Player Camera, named "Main Camera no-post-processing"
GameObject SmartphoneCaptureCamera; //the GameObject, named "SmartphoneCaptureCamera" is a Camera with a CullingMask where the Layer "hands" is excluded ("_HANDMENU" and "XR Interactions Hands Setup" are on the "hands" layer)
// Update is called once per frame
void Update()
{
//StartCoroutine(TakeScreenshot());
2025-02-06 07:33:52 +01:00
/* if (Input.GetKeyDown(KeyCode.P))
{
StartCoroutine(TakeScreenshot());
}
*/
}
public void OnClickTakeScreenshot()
{
StartCoroutine(TakeScreenshot());
}
public void OnClickPreview()
{
StartCoroutine(CreatePreview());
}
public void DestroyPreviewTexture()
{
if (_previewImg.texture == true)
{
Destroy(_previewImg.texture);
}
}
IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
string timpStamp = DateTime.Now.ToString("ddMMyyyyHHmmssffff");
ScreenCapture.CaptureScreenshot("snapshot_" + timpStamp + ".png");
}
IEnumerator CreatePreview()
{
yield return new WaitForEndOfFrame();
2025-02-06 07:33:52 +01:00
// find all Cameras:
XRCamera = GameObject.Find("Main Camera");
XRHoloVideoCamera = GameObject.Find("Main Camera no-post-processing");
SmartphoneCaptureCamera = GameObject.Find("SmartphoneCaptureCamera");
// get the Camera components:
Camera vrcam = XRCamera.GetComponent<Camera>();
Camera vrcamnopost = XRHoloVideoCamera.GetComponent<Camera>();
Camera capturecam = SmartphoneCaptureCamera.GetComponent<Camera>();
// switch active Cameras
vrcam.enabled = false;
vrcamnopost.enabled = false;
capturecam.enabled = true;
// call the function to take actual screenshot
_previewImg.texture = ScreenCapture.CaptureScreenshotAsTexture();
2025-02-06 07:33:52 +01:00
// switch active Cameras back
vrcam.enabled = true;
vrcamnopost.enabled = true;
capturecam.enabled = false;
}
}