Repeating "Application.CaptureScreenshot" in loop


I use this script on a camera, I want it to take a screenshot every x amount of degrees. However right now only one image is saved not the 10 or so the scripts is supposed to take. Probably missing something obvious here, any ideas ?

I tried playing around with coroutines because maybe “Application.CaptureScreenshot” takes too long but I couldn’t figure that stuff out.

using UnityEngine;
using System.Collections;

public class screenshot : MonoBehaviour 
	Vector3 RotatieVar = new Vector3 (0,10,0); //rotation variable
	string Opslaan = ""; // empty directory variable
	void Update ()
		if (Input.GetKeyDown ("f6")) //start the script 
			for(int i = 0; i < 360; i=i+36)
				Opslaan = "C:/Users/Huib/Desktop/screenshot/screenshot_" + i + ".png"; //save image in / as
				Application.CaptureScreenshot(Opslaan,superSize:0); //take screenshot

				transform.Rotate (RotatieVar); //rotate camera

I’m not sure about this, but I tink that the resulting images should all be the same if you managed to create them in the first place. This because rendering is done after the Update, so when you rotate an object 10 times in one frame, only either the first or last rotation should be able to get captured.

Coroutines should help with that problem, but the problem you describe (only 1 result) is a bit odd.

What is the actual resulting path of the created image? And how many times is the code inside the loop actually executed?

Also, reading this blogpost, perhaps you’ll have more luck with Texture2D.ReadPixels and Texture2D.EncodeToPNG.

Using the above, I created the code below which does create 10 screenshots. It takes a few frames to execute though, because it need to render each new rotation before taking a screenshot. Perhaps you could improve this by not using a coroutine and calling camera.Render in your for loop. But I haven’t tested anything like that yet.

public class ScreenshotTest : MonoBehaviour
	// Use this for initialization
	void Start ()
        StartCoroutine(CreateScreenshots(Application.dataPath + "/SavedScreen"));

    private IEnumerator CreateScreenshots(string path)
        for (int i = 0; i < 360; i = i + 36)
            // Wait untill we actually rendered
            yield return new WaitForEndOfFrame();

            Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

            // Read screen contents into the texture
            texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);

            // Write to file
            byte[] bytes = texture.EncodeToPNG();
            System.IO.File.WriteAllBytes(path + i + ".png", bytes);

            // Clean up the used texture

            // Do your rotating here
            transform.Rotate(new Vector3(0, 10, 0));