Search Unity

Automatically take screen capture of multiple scenes via script

Discussion in 'Editor & General Support' started by k_koehler, Sep 4, 2014.

  1. k_koehler

    k_koehler

    Joined:
    Jan 17, 2014
    Posts:
    5
    I am attempting to take high resolution screen captures of a large number of scenes in Unity without having to manually open each scene myself to initiate some sort of script to take the screen capture. If you have any advice for how to do this other than what I've shown below, I'm all ears. Otherwise, I've included my script along with the problem I'm having. I'mvery new to programming in C# and am open to any and all suggestions.

    This script successfully opens each scene, because I can see it cycle through the filenames on the top left of the menu bar. However, the only scene that enters play mode is the final scene. The screen capture script is attached to a camera in every scene and successfully takes a screen capture (using EditorApplication.CaptureScreenshot) within the Awake method once play mode is entered. It seems like it needs to pause and wait for play mode to be entered and for the screen capture to be taken before moving on. I've tried while loops with various conditionals and co-routines with no success.

    A bit of additional info: The script is in the Editor folder in my project, so it creates a menu item that I can click within the editor to initiate it. I automatically baked all the lights for these scenes using a similar implementation, but I can't get this to work, and I have no idea why.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6. public class TakeCaptures : MonoBehaviour
    7. {
    8.    
    9.     [MenuItem("SaveLighting/Take Captures")]
    10.    
    11.     static void TakeIt()
    12.     {
    13.        
    14.         DirectoryInfo dir = new DirectoryInfo ("Assets/_CompleteScenes");
    15.         DirectoryInfo[] info = dir.GetDirectories ();
    16.         foreach (DirectoryInfo f in info)
    17.         {
    18.            
    19.             DirectoryInfo dir2 = new DirectoryInfo (f.FullName);
    20.             FileInfo[] info2 = dir2.GetFiles("*bake.unity");
    21.             foreach (FileInfo ff in info2)
    22.             {
    23.                
    24.                 string filename = ff.FullName.ToString ();
    25.                 EditorApplication.OpenScene (filename);
    26.                 EditorApplication.isPlaying = true;
    27.                
    28.             }
    29.         }
    30.     }
    31. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    At line 27 add a wait for 5 seconds. Currently you start the game playing, and then open the next scene.
     
  3. k_koehler

    k_koehler

    Joined:
    Jan 17, 2014
    Posts:
    5
    What function should I use to wait? I tried to use yield return new WaitForSeconds at that line, but it didn't work with my static function.
     
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    Create a new function that has a return type of IEnumerator and have that do the WaitForSeconds() for you.
     
  5. k_koehler

    k_koehler

    Joined:
    Jan 17, 2014
    Posts:
    5
    Okay, so I tried this to the best of my knowledge. I'll attach the updated script. The error I am getting is "An object reference is required to access non-static member"

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.IO;
    6.  
    7. public class TakeCaptures : MonoBehaviour
    8. {
    9.  
    10.     [MenuItem("SaveLighting/Take Captures")]
    11.  
    12.     static void TakeIt()
    13.     {
    14.      
    15.         DirectoryInfo dir = new DirectoryInfo ("Assets/_CompleteScenes");
    16.         DirectoryInfo[] info = dir.GetDirectories ();
    17.         foreach (DirectoryInfo f in info)
    18.         {
    19.          
    20.             DirectoryInfo dir2 = new DirectoryInfo (f.FullName);
    21.             FileInfo[] info2 = dir2.GetFiles("*bake.unity");
    22.             foreach (FileInfo ff in info2)
    23.             {
    24.              
    25.                 string filename = ff.FullName.ToString ();
    26.                 EditorApplication.OpenScene (filename);
    27.                 EditorApplication.isPlaying = true;
    28.                 StartCoroutine(Wait(5f));
    29.             }
    30.         }
    31.     }
    32.     IEnumerator Wait(float duration)
    33.     {
    34.         yield return new WaitForSeconds(duration);
    35.     }
    36. }
    37.  
     
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    Try line 28:

    Code (csharp):
    1.     Wait(5f);
     
  7. k_koehler

    k_koehler

    Joined:
    Jan 17, 2014
    Posts:
    5
    Unity still doesn't like it: "An object reference is required to access non-static member" at that line.

    Thank you so much for the help so far.
     
  8. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    Hmm. I can't make changes to that function in order to make a delay work. Maybe instead, take a look at:

    http://docs.unity3d.com/ScriptReference/EditorApplication-timeSinceStartup.html

    That shows an example which does actions after a certain amount of time. I think if I was in your shoes, I'd consider making a window that has your list of scenes, and switches the editor to them one at a time. (I know this is quite a change for you.)
     
  9. k_koehler

    k_koehler

    Joined:
    Jan 17, 2014
    Posts:
    5
    Thanks again for your help. I've been going through a lot of different solutions over the past week and I finally found one that worked. I basically ended up adding all of my scenes as levels in the build settings and sequentially loading levels and working with scripts that are attached to objects in the scenes to avoid the issues with static methods.

    In case others are interested, here is some more detail: As I said, I added each scene as a level to the project. In the build settings, each level gets assigned an integer value that can be used with Application.LoadLevel. I attached the script below to an empty game object in one of the scenes/levels. It is set so that it doesn't get destroyed when a new level loads. It sequentially loads every level and uses yield to wait for the screen capture to be taken (a screen capture script is attached to a camera in every scene/level).

    This is the empty game object script. When I play the scene it is in, it starts the level loading process. So far I have only tested it on a subset of 4 scenes, but I expect it should work for the larger set.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6. public class initiateCaptures : MonoBehaviour {
    7.     void Awake ()
    8.     {
    9.      
    10.         DontDestroyOnLoad(transform.gameObject);
    11.     }
    12.  
    13.     IEnumerator Start()
    14.     {
    15.         for (int i = 0; i <= 3; i++)
    16.         {
    17.             Debug.Log(i);
    18.             yield return new WaitForSeconds(20f);
    19.             Debug.Log("loading level");
    20.             Application.LoadLevel(i);
    21.             Debug.Log("level loaded");
    22.             Debug.Log(i);
    23.         }
    24.     }
    25. }
    26.  
    And just for good measure, here is the screen capture script attached to the camera:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.IO;
    6.  
    7. public class ScreenCapture : MonoBehaviour {
    8.     public void Awake() {
    9.         int superSize = 3;
    10.         string filepath = EditorApplication.currentScene;
    11.         string levelname = Application.loadedLevelName;
    12.         filepath = filepath.Remove (filepath.Length-11,11);
    13.         string name = "screenshot.png";
    14.         string filename = filepath + levelname + name;
    15.         Debug.Log(filename);
    16.         Application.CaptureScreenshot(filename,superSize);
    17.         Debug.Log ("screenshot taken");
    18.  
    19.     }
    20. }
    21.