Search Unity

screenshot scene problem

Discussion in 'Scripting' started by unity_2AbA_4xiuH7rTg, Jan 14, 2021.

?

screenshot scene

  1. error

    0 vote(s)
    0.0%
  2. int

    1 vote(s)
    100.0%
  1. unity_2AbA_4xiuH7rTg

    unity_2AbA_4xiuH7rTg

    Joined:
    Jan 14, 2021
    Posts:
    1
    Hello,

    I want to create an application that makes 500 random .png or .jpeg pictures of a camera view of my scene.

    For this I wanted to cut the work:
    at first I focused on the rotation of my random scene.
    In a second step I wanted to work on the automatic screenshot and the creation of an image.
    In a third step I wanted to bring everything together.

    Except that I already have a problem with the screenshot.
    For this part, I want to have a C# that runs and makes screenshot of the scene.

    Do you have any ideas to help me ? thank you in advance...

    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     public Snapshotcamera snapCam;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(keyCode.Space))
    14.         {
    15.             snapCam.CallTakeSnapshot();
    16.         }
    17.     }
    18. }
    19.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Camera))]
    6. public class Snapshotcamera : MonoBehaviour
    7. {
    8.  
    9.     Camera snapCam;
    10.     int resWidth = 256;
    11.     int resHeight = 256;
    12.  
    13.     void Awake()
    14.     {
    15.         snapCam = GetComponent<Camera>();
    16.         if (snapCam.targetTexture ==null)
    17.         {
    18.             snapCam.targetTexture = new RenderTexture(resWidth, resHeight, 24);
    19.         }
    20.         else
    21.         {
    22.             resWidth = snapCam.targetTexture.width;
    23.             resHeight = snapCam.targetTexture.height;
    24.         }
    25.         snapCam.gameObject.SetActive(false);
    26.     }
    27.  
    28.     public void CallTakeSnapshot()
    29.     {
    30.         snapCam.gameObject.SetActive(true);
    31.     }
    32.  
    33.     void LateUpdate()
    34.     {
    35.         if (snapCam.gameObject.activeInHierarchy)
    36.         {
    37.             Texture2D snapshot = new Texture2D(resWidth, resHeight, TextureFormat.RBG24, false);
    38.             snapCam.Render();
    39.             RenderTexture.active = snapCam.targetTexture;
    40.             snapshot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    41.             byte[] bytes = snapshot.EncodeToPNG();
    42.             string filename = SnapshotName();
    43.             System.IO.File.WriteAllBytes(filename, bytes);
    44.             Debug.Log("Snapshot taken!");
    45.             snapCam.gameObject.SetActive(false);
    46.         }
    47.     }
    48.     string SnapshotName()
    49.     {
    50.         return string.Format("{0}/Snapshots/snap {1}x{2}_{3}.png",
    51.             Application.dataPath,
    52.             resWidth,
    53.             resHeight,
    54.             System.DateTime.Now.ToString("yyy-MM-dd_HH-mm-ss"));
    55.     }
    56. }
    57.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Absolutely no idea at all since you don't say what the problem is.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    Also there is a class called ScreenCapture that might help you.

    And please, just resist the urge to add irrelevant polls to the post. It helps not at all.
     
    seejayjames likes this.