Search Unity

Resolved Why is this only making one screenshot per session?

Discussion in 'Scripting' started by Ne0mega, Apr 8, 2021.

  1. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    Pretty much this: https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToPNG.html

    but it only takes one screenshot. I think it may be coroutine mumbo jumbo, which I dont get and use often, causing the problem, but am not sure: (mostly because I dont get/use coroutines very often).

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. using System;
    6.  
    7. public class Screenshotter : MonoBehaviour
    8. {
    9.     [TextArea(0, 0)]
    10.     public string ctrl_Shift_S = "ctrl + shift + s pressed for screenshot";
    11.  
    12.     void Update()
    13.     {
    14.         if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
    15.             && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
    16.             && Input.GetKeyUp(KeyCode.S))
    17.         {
    18.             Debug.Log(ctrl_Shift_S);
    19.             Start();
    20.         }
    21.     }
    22.     // Take a shot immediately
    23.     IEnumerator Start()
    24.     {
    25.         yield return UploadPNG();
    26.     }
    27.  
    28.     IEnumerator UploadPNG()
    29.     {
    30.         // We should only read the screen buffer after rendering is complete
    31.         yield return new WaitForEndOfFrame();
    32.  
    33.         // Create a texture the size of the screen, RGB24 format
    34.         int width = Screen.width;
    35.         int height = Screen.height;
    36.         Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGB24, false);
    37.  
    38.         // Read screen contents into the texture
    39.         texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    40.         texture2D.Apply();
    41.  
    42.         byte[] bytes = texture2D.EncodeToPNG();
    43.         UnityEngine.Object.Destroy(texture2D);
    44.  
    45.         string dateTimeString = DateTime.Now.ToString("yyyyMMddHHmmss");
    46.         File.WriteAllBytes(Application.dataPath + "/Screenshots/" + dateTimeString + "_SavedScreen.png", bytes);
    47.     }
    48. }
    49.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Have you intentionally used a method named "Start", realizing there's a built-in Unity method also named Start()? Was it your intention?

    I imagine what you're missing is just wrapping the call to your UploadPNG in StartCoroutine().

    What happens if you totally get rid of your Start() method, and change line 19 to
    StartCoroutine(UploadPNG()) 
    ?
     
    Ne0mega and PraetorBlue like this.
  3. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    That's how the Unity Manual did it. I dunno why.

    https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToPNG.html

    StartCoRoutine(UploadPNG()) is the syntax I think I am looking for.

    EDIT: yup, that was it. StartCoroutine() yup, uh huh.
     
    Last edited: Apr 9, 2021
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,792
    There's nothing in the coroutine that loops, why are you expecting it to run more than once?

    EDIT: NVM, I misunderstood the issue.