Getting screenshots within a loop?

Hey guys, I’m somewhat new to using Unity and I need to get a bunch of still images of the objects I’m using in my environment from different angles.

Some details: I have a game object hierarchy that I’ve preserved for filenaming purposes:

Level 1. Objects (parent) - I’ve attached the script here.

Level 2. Object subclasses (children) - again, to organize classes for filenaming.

Level 3. Objects (grandchildren) - the actual objects I need images of.

So, I am looping through the children, then through the grandchildren, moving each grandchild to a predetermined location that the camera is looking at. Once at that location, I rotate the object the appropriate angle in a for loop and take a screenshot. I then rotate it back to its original rotation (0,0,0) each iteration. After the rotation loop executes I then move the object back to its original position and then go to the next object.

The main problem I am having is that it doesn’t seem to be looping through each item and taking a screenshot. It appears as if it will loop through everything and then take one screenshot at the end instead of taking a screenshot every iteration. Essentially, what I end up with is a picture of a blank screen, because the objects have all been moved into the camera view and then returned to their original position before the camera even does a screen capture…

Any assistance would be greatly appreciated!

Code:

var seatPos : Vector3;

function Start () {
	var nSubTypes = transform.childCount;
	// Loop through object subtypes
	for (var i : int = 0; i < nSubTypes; i++) {
		
		var Child = transform.GetChild(i);
		var nChildren = Child.childCount;

		// Loop through each subobject
		for (var j : int = 0; j < nChildren; j++) {
		
			// Save original position
			var origPos = Child.GetChild(j).transform.position;
			
			// Move to camera position
			Child.GetChild(j).transform.position = seatPos;
			
			// Make array of camera angles
			var angles = new Array();
			angles[0] = -90;
			angles[1] = -60;
			angles[2] = -30;
			angles[3] = 0;
			angles[4] = 30;
			angles[5] = 60;
			angles[6] = 90;
			
			var nAngles = angles.length;
			for (var k : int = 0; k < nAngles; k++) {
			
				// Make filename
				var str = "Screenshots/ScreenshotTest_" + Child.name + "_" + Child.GetChild(j).name + "_" + angles[k] + ".png";
				
				// Rotate
				Child.GetChild(j).transform.Rotate(0,angles[k],0);
				
				// Screenshot
				Application.CaptureScreenshot(str,5);
				
				Debug.Log(Child.GetChild(j).name + " angle " + angles[k] + " captured...");
				
				// Rotate back
				Child.GetChild(j).transform.Rotate(0,0,0);
				
				WaitForSeconds(3);
				
			}
			
			// Move to original position
			Child.GetChild(j).transform.position = origPos;
		}
	}
}

Its a timing problem:
The order is : Update → Rendering → saving the screenshot.
You can only take one screenshot per frame if you are using the Application.CaptureScreenshot function.
if its not important hat this all screenshots will be taken in one frame you can put it into a co-routine.
Set camera position → Save screen-shoot → wait a frame and from the beginning.

A not tested approach could be to to generate for each position a new camera that will render into a seperate render-texture and then copy the content of the render-texture to to a normal texture2d and the save each of them to disk after converting it via Texture2D.EncodeToPNG or Texture2D.EncodeToJPG. That would be kind of resource intensive but could work in one frame.

Just to follow up, I took fffMalzbier’s advice and made a coroutine. I was still having trouble getting it to behave within update, but putting it into the Start function made it work correctly, with the addition of some well-placed yields (maybe I used too many?).

Code:

var seatPos : Vector3;

function Start () {
	GetScreen();
}

function GetScreen () {
	var nSubTypes = transform.childCount;
	// Loop through object subtypes
	for (var i : int = 0; i < nSubTypes; i++) {
		var Child = transform.GetChild(i);
		var nChildren = Child.childCount;

		// Loop through each subobject
		for (var j : int = 0; j < nChildren; j++) {
		
			// Save original position
			var origPos : Vector3 = Child.GetChild(j).transform.position;
			
			// Move to camera position
			Child.GetChild(j).transform.position = seatPos;
			
			// Make array of camera angles
			var angles = new Array();
			angles[0] = 0;
			angles[1] = -30;
			angles[2] = -30;
			angles[3] = -30;
			angles[4] = 180;
			angles[5] = -30;
			angles[6] = -30;
			
			var angleName = new Array();
			angleName[0] = 90;
			angleName[1] = 120;
			angleName[2] = 150;
			angleName[3] = 180;
			angleName[4] = 0;
			angleName[5] = 30;
			angleName[6] = 60;
			
			var nAngles = angles.length;
			for (var k : int = 0; k < nAngles; k++) {
				yield;
				// Make filename
				var str = "Screenshots/ScreenshotTest_" + Child.name + "_" + Child.GetChild(j).name + "_" + angleName[k] + ".png";
				
				// Rotate
				Child.GetChild(j).transform.Rotate(0,angles[k],0);
				
				// Screenshot
				Application.CaptureScreenshot(str,5);
				
				Debug.Log(Child.GetChild(j).name + " angle " + angles[k] + " captured...");
				
				// Rotate back
				yield;
				Child.GetChild(j).transform.Rotate(0,0,0);
			}
			
			// Move to original position
			yield;
			Child.GetChild(j).transform.position = origPos;
		}
	}
}