Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I take screencapture in batchmode

Discussion in 'Scripting' started by _arnaud_, Jul 18, 2020.

  1. _arnaud_

    _arnaud_

    Joined:
    Dec 11, 2013
    Posts:
    4
    I have a simple project with a scene that I can run through the Unity app. I am now trying to take and save a simple screenshot in batchmode, but to no avail.

    Here is the code I use:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;

    public class TakeAShot: MonoBehaviour
    {
    public static void MyGameMethod()
    {
    string imagePath = "Assets/Resources/test.png";
    ScreenCapture.CaptureScreenshot(imagePath);
    }
    }


    Here is the command I am running:

    /Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity -batchmode  -projectPath /Users/mikemike/MyFirstGame -executeMethod TakeAShot.MyGameMethod -quit


    The command does not error out, but no screenshot is being saved.

    Thanks in advance for your help
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    I suspect the issue here is that you are passing a relative path to CaptureScreenshot and it is being saved in an unexpected location. I would suggest something like this to save it in the application specific folder on your OS:
    Code (CSharp):
    1.                
    2. var folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.productName, "Screenshots");
    3. ScreenCapture.CaptureScreenshot(path);
    4.  
    On windows, for example, it would be then saved here (generally):

    C:\Users\YourUser\AppData\Roaming\YourGame\Screenshots