Search Unity

Question Any ideas on changing this script to make it possible to capture a screenshot of my scene in VR?

Discussion in 'VR' started by vardnan, Mar 15, 2023.

  1. vardnan

    vardnan

    Joined:
    Apr 16, 2022
    Posts:
    5
    I am trying to make a script allowing me to take a screenshot of my scene in virtual reality (for Quest 2) through the main camera of my XR Rig (Using XR Interaction Toolkit). Then I want to display that captured photo to the user for a few seconds before it is removed. Ideally, this photo would be saved in the file system of the Quest 2 device.

    I figured out how to build this functionality for a desktop game, but I have challenges editing this script to work in virtual reality.

    With the current script, everything works as intended, except the photo stays "blue" when displayed to the user in VR.

    I would appreciate any helpful input.

    Here is my current script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PhotoCapture : MonoBehaviour
    7. {
    8.  
    9.     [Header("Photo taker")]
    10.     [SerializeField] private Image photoDisplayArea;
    11.     [SerializeField] private GameObject photoFrame;
    12.     [SerializeField] private GameObject cameraUI;
    13.  
    14.     [Header("Flash effect")]
    15.     [SerializeField] private GameObject cameraFlash;
    16.     [SerializeField] private float flashTime;
    17.  
    18.  
    19.     [Header("Photo Fader Effect")]
    20.     [SerializeField] private Animator fadingAnimation;
    21.  
    22.     [Header("Audio")]
    23.     [SerializeField] private AudioSource cameraAudio;
    24.  
    25.     private Texture2D screenCapture;
    26.     private bool viewingPhoto;
    27.  
    28.     private void Start()
    29.     {
    30.         //screenCapture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    31.         screenCapture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
    32.     }
    33.  
    34.     private void Update()
    35.     {
    36.         if (Input.GetMouseButtonDown(0))
    37.         {
    38.             if (!viewingPhoto)
    39.             {
    40.                 StartCoroutine(CapturePhoto());
    41.             }
    42.  
    43.             else
    44.             {
    45.                 RemovePhoto();
    46.             }
    47.         }
    48.     }
    49.  
    50.     public void TakePhoto()
    51.     {
    52.  
    53.         if (!viewingPhoto)
    54.         {
    55.             StartCoroutine(CapturePhoto());
    56.         }
    57.  
    58.         else
    59.         {
    60.             RemovePhoto();
    61.         }
    62.     }
    63.  
    64.  
    65.     IEnumerator CapturePhoto()
    66.     {
    67.         cameraUI.SetActive(false);
    68.         viewingPhoto = true;
    69.  
    70.         yield return new WaitForEndOfFrame();
    71.  
    72.         Rect regionToRead = new Rect(0, 0, Screen.width, Screen.height);
    73.  
    74.         screenCapture.ReadPixels(regionToRead, 0, 0, false);
    75.         screenCapture.Apply();
    76.         ShowPhoto();
    77.     }
    78.  
    79.     void ShowPhoto()
    80.     {
    81.  
    82.         StartCoroutine(CameraFlashEffect());
    83.  
    84.         Sprite photoSprite = Sprite.Create(screenCapture, new Rect(0.0f, 0.0f, screenCapture.width, screenCapture.height), new Vector2(0.5f, 0.5f), 100.0f);
    85.         photoDisplayArea.sprite = photoSprite;
    86.         photoFrame.SetActive(true);
    87.         fadingAnimation.Play("PhotoFade");
    88.  
    89.     }
    90.  
    91.     IEnumerator CameraFlashEffect()
    92.     {
    93.         cameraAudio.Play();
    94.         cameraFlash.SetActive(true);
    95.         yield return new WaitForSeconds(flashTime);
    96.         cameraFlash.SetActive(false);
    97.     }
    98.  
    99.     void RemovePhoto()
    100.     {
    101.         viewingPhoto = false;
    102.         photoFrame.SetActive(false);
    103.         cameraUI.SetActive(true);
    104.  
    105.     }
    106. }