Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

*SOLVED* Sprite2D not updating till after alt+tab

Discussion in 'AR/VR (XR) Discussion' started by Reshh_, Mar 8, 2019.

  1. Reshh_

    Reshh_

    Joined:
    Mar 8, 2019
    Posts:
    4
    Hi all,

    I am working on a VR project in which I am making a virtual camera for a sort of smart phone the player hold in their left hand. Once the player takes a picture the picture should be previewed and the player can decide to send it to the other player or not.
    This is where the problem arises. Once a picture is taken and the preview is shown, the preview is that of the picture taken before the current one. The preview does not update until I alt tab and go back into Unity.

    Any ideas on how to fix this?

    Code used (this is taken from a thread on how to take a screenshot through a camera):
    Code (CSharp):
    1. public IEnumerator TakeScreenShot() {
    2.         yield return new WaitForEndOfFrame();
    3.        
    4.         Camera camOV = virtualCamera.GetComponent<Camera>();
    5.         RenderTexture currentRT = RenderTexture.active;
    6.         RenderTexture.active = camOV.targetTexture;
    7.         camOV.Render();
    8.         Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, TextureFormat.RGB24, false);
    9.         imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 0);
    10.         imageOverview.Apply();
    11.         RenderTexture.active = currentRT;
    12.  
    13.         // Encode texture into PNG
    14.         byte[] bytes = imageOverview.EncodeToPNG();
    15.  
    16.         // save in memory
    17.         string filename = "screenshot.png";
    18.         path = "*path*/Snapshots/" + filename;
    19.         // Write to path (previous screenshots are overwritten)
    20.         File.WriteAllBytes(path, bytes);
    21.     }
    Screenshots of camera preview:
    VirtualCamera0.PNG
    Above is just the normal camera with a render texture showing the camera's real time view
    VirtualCamera1.PNG
    Above is the preview image right after pressing take picture in the screenshot before this one. It is clearly showing a different angle
    VirtualCamera2.PNG
    And above is the preview after an alt tab away from and back into Unity, now showing the correct image.
     
  2. Reshh_

    Reshh_

    Joined:
    Mar 8, 2019
    Posts:
    4
    *****UPDATE*****
    Now the image no longer updates after an alt tab away from and back to Unity. HOWEVER I managed to write some code that creates the sprite and then sets it to the image. This makes it so the preview image now always lags behind on the actual picture taken. So:
    • First pic taken -> preview is blank
    • Second pic taken -> first pic in preview
    • Third pic taken -> second pic in preview
    • etc...
    Here's the code I took from another forum on how to load a texture from a file and set that to a sprite (and then set that sprite to the preview image):
    Code (CSharp):
    1. private Sprite MakeSprite() {
    2.         Sprite sprite;
    3.         Texture2D spriteTexture = LoadTexture(path);
    4.         sprite = Sprite.Create(spriteTexture, new Rect(0, 0, 500, 1000), new Vector2(0, 0), 100f, 0, SpriteMeshType.Tight);
    5.  
    6.         return sprite;
    7.     }
    8.  
    9.     private Texture2D LoadTexture(string FilePath) {
    10.  
    11.         // Load a PNG or JPG file from disk to a Texture2D
    12.         // Returns null if load fails
    13.  
    14.         Texture2D Tex2D;
    15.         byte[] FileData;
    16.  
    17.         if (File.Exists(FilePath)) {
    18.             FileData = File.ReadAllBytes(FilePath);
    19.             Tex2D = new Texture2D(2, 2);           // Create new "empty" texture
    20.             if (Tex2D.LoadImage(FileData))           // Load the imagedata into the texture (size is set automatically)
    21.                 return Tex2D;                 // If data = readable -> return texture
    22.         }
    23.         return null;                     // Return null if load failed
    24.     }
    25.  
    26. //--------------The area where this function is used-------------------
    27.  
    28. private void TakePicture() {
    29.         // Start screenshot coroutine
    30.         StartCoroutine(TakeScreenShot());
    31.         // Create sprite
    32.         previewSprite = MakeSprite();
    33.         // Set sprite to preview image
    34.         preview.GetComponent<Image>().sprite = previewSprite;
    35.         // Set panel with preview active
    36.         confirmPanel.SetActive(true);
    37.     }
     
  3. Reshh_

    Reshh_

    Joined:
    Mar 8, 2019
    Posts:
    4
    *****************SOLVED********************
    The reason the picture preview was out of sync was because the sprite was being created while the screenshot coroutine wasn't finished yet. So now the coroutine calls the function that sets the preview. This means that the preview is only set once the screenshot has been taken. So now it works.
     
    JoeStrout likes this.