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

Headless scripting

Discussion in 'Scripting' started by adlambert, Sep 17, 2022.

  1. adlambert

    adlambert

    Joined:
    Sep 17, 2022
    Posts:
    2
    I am not getting very far looking at the api documentation pages, I need to know if I can use scripting with Unity to do the following unusual requirement:

    * run a script against Unity that will move a viewpoint/camera to a specified (within the script) position within a height map terrain. Then the view of that terrain is rendered as a single frame and save to disk as an image file.

    * this would happen headless, with no display. So a server running unity would pick up the script, run it and perform the operation above. The only output would be the saved image.

    This is an unusual use case so the documentation doesn’t really cover it but I am hoping that somebody here might have seen something like this before?

    thanks you looking!
     
  2. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    This might help possibly


    Any way here is my interpretation of a screen shot thingy

    Code (CSharp):
    1. using System.IO;
    2. using UnityEngine;
    3.  
    4. public class ScreenShotControl : MonoBehaviour
    5. {
    6.     public Camera shotCam;
    7.  
    8.     public int imageHeight;
    9.     public int imageWidth;
    10.  
    11.     public string folderName;
    12.     public string fileName;
    13.  
    14.     void Start()
    15.     {
    16.         //setup the cameras target
    17.         shotCam.targetTexture = new RenderTexture(imageWidth, imageHeight, 24);
    18.  
    19.         //set cams position and then...
    20.         TakePicture();
    21.     }
    22.  
    23.     public void TakePicture()
    24.     {
    25.         //Render the camera and set the current active render
    26.         shotCam.Render();
    27.         RenderTexture.active = shotCam.targetTexture;
    28.  
    29.         //save the RenderTexture to a texture
    30.         Texture2D screenShot = new(imageWidth, imageHeight, TextureFormat.RGB24, false);
    31.         screenShot.ReadPixels(new Rect(0, 0, imageWidth, imageHeight), 0, 0);
    32.  
    33.         //Create a byte array and save the data to disk
    34.         byte[] saveData = screenShot.EncodeToPNG();
    35.  
    36.         //make sure the folder exists and write the file
    37.         if (!Directory.Exists(Application.dataPath + "/" + folderName)) Directory.CreateDirectory(Application.dataPath + "/" + folderName);
    38.         File.WriteAllBytes(FileNameTimeStamp(), saveData);
    39.     }
    40.  
    41.     private string FileNameTimeStamp()
    42.     {
    43.         return Application.dataPath + "/" + folderName + "/" + fileName + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
    44.     }
    45. }
     
    Last edited: Sep 18, 2022
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    It depends on where you want to run your script. The build needs a GPU + a Window (context) to render to. So your server needs to have a GPU that's for sure.

    For the context (window), if you build for Linux, this might be possible. All you need is to have a virtual display by using X server for eg.

    Check this article. The part you're interested into is probably "Running Unity app with graphics on a server without monitor" or "Run Unity app with graphics on Amazon Cloud!"

    https://towardsdatascience.com/how-to-run-unity-on-amazon-cloud-or-without-monitor-3c10ce022639
     
    Last edited: Sep 18, 2022
  4. adlambert

    adlambert

    Joined:
    Sep 17, 2022
    Posts:
    2
    Two fine quality replies. Thank you both!
     
    pantang and Nad_B like this.