screenshot scene problem

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:

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


public class Player : MonoBehaviour
{
    public Snapshotcamera snapCam;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(keyCode.Space))
        {
            snapCam.CallTakeSnapshot();
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class Snapshotcamera : MonoBehaviour
{

    Camera snapCam;
    int resWidth = 256;
    int resHeight = 256;

    void Awake()
    {
        snapCam = GetComponent<Camera>();
        if (snapCam.targetTexture ==null)
        {
            snapCam.targetTexture = new RenderTexture(resWidth, resHeight, 24);
        }
        else
        {
            resWidth = snapCam.targetTexture.width;
            resHeight = snapCam.targetTexture.height;
        }
        snapCam.gameObject.SetActive(false);
    }

    public void CallTakeSnapshot()
    {
        snapCam.gameObject.SetActive(true);
    }

    void LateUpdate()
    {
        if (snapCam.gameObject.activeInHierarchy)
        {
            Texture2D snapshot = new Texture2D(resWidth, resHeight, TextureFormat.RBG24, false);
            snapCam.Render();
            RenderTexture.active = snapCam.targetTexture;
            snapshot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
            byte[] bytes = snapshot.EncodeToPNG();
            string filename = SnapshotName();
            System.IO.File.WriteAllBytes(filename, bytes);
            Debug.Log("Snapshot taken!");
            snapCam.gameObject.SetActive(false);
        }
    }
    string SnapshotName()
    {
        return string.Format("{0}/Snapshots/snap {1}x{2}_{3}.png",
            Application.dataPath,
            resWidth,
            resHeight,
            System.DateTime.Now.ToString("yyy-MM-dd_HH-mm-ss"));
    }
}

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.

1 Like