2024-06-21 17:11:07 +02:00
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine ;
using System ;
2024-07-15 15:55:13 +02:00
using UnityEngine.UI ;
2025-02-06 07:33:52 +01:00
using Oculus.Platform ;
2024-06-21 17:11:07 +02:00
public class Screenshot : MonoBehaviour
{
2024-07-15 15:55:13 +02:00
[SerializeField]
RawImage _previewImg ;
2024-06-21 17:11:07 +02:00
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)
2024-06-21 17:11:07 +02:00
// 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 ( ) ) ;
}
* /
2024-06-21 17:11:07 +02:00
}
public void OnClickTakeScreenshot ( )
{
StartCoroutine ( TakeScreenshot ( ) ) ;
}
2024-07-15 15:55:13 +02:00
public void OnClickPreview ( )
{
StartCoroutine ( CreatePreview ( ) ) ;
}
public void DestroyPreviewTexture ( )
{
if ( _previewImg . texture = = true )
{
Destroy ( _previewImg . texture ) ;
}
}
2024-06-21 17:11:07 +02:00
IEnumerator TakeScreenshot ( )
{
yield return new WaitForEndOfFrame ( ) ;
string timpStamp = DateTime . Now . ToString ( "ddMMyyyyHHmmssffff" ) ;
ScreenCapture . CaptureScreenshot ( "snapshot_" + timpStamp + ".png" ) ;
}
2024-07-15 15:55:13 +02:00
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
2024-07-15 15:55:13 +02:00
_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 ;
2024-07-15 15:55:13 +02:00
}
2024-06-21 17:11:07 +02:00
}