Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by angelonc · Mar 13, 2015 at 08:44 AM · transformparentchildrenscreenshotfor loop

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;
         }
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fffMalzbier · Mar 13, 2015 at 08:53 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image angelonc · Mar 13, 2015 at 05:56 PM 0
Share

Thanks! Got it working now using your suggestion

avatar image
0

Answer by angelonc · Mar 14, 2015 at 01:37 AM

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to move the parent object to a child of one of its children? 2 Answers

all parents of a transform 1 Answer

Move parent so children align with other obj! 1 Answer

How to move the parent object to a child of one of its children? 1 Answer

Accessing children of instances vs children of original prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges