Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2017 19:18
Show Gist options
  • Select an option

  • Save anonymous/bfbf7dcc4e7dbbc28a8ac33aa67669e9 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/bfbf7dcc4e7dbbc28a8ac33aa67669e9 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 21, 2017.
    53 changes: 53 additions & 0 deletions CameraDump.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    [RequireComponent(typeof(Camera))]
    public class CameraDump : MonoBehaviour
    {
    private int imageCount;

    private RenderTexture targetTexture;
    private const string folderName = "CameraDump";
    private string dumpDirectory
    {
    get
    {
    return Application.dataPath + @"/../" + folderName + @"/";
    }
    }
    private string dumpPrefix
    {
    get
    {
    return gameObject.name + "/";
    }
    }


    // hook and initialize
    private void Start()
    {
    // check for path validity
    System.IO.Directory.CreateDirectory(dumpDirectory + dumpPrefix);
    }

    // dump
    private void OnPostRender()
    {
    Texture2D stagedTexture = new Texture2D(Screen.width, Screen.height);

    stagedTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height),
    0,
    0);

    stagedTexture.Apply();

    var pngData = stagedTexture.EncodeToPNG();

    string filePath = dumpDirectory + dumpPrefix + imageCount++ + ".png";

    System.IO.File.WriteAllBytes(filePath, pngData);
    }
    }
    28 changes: 28 additions & 0 deletions CaptureSettings.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CaptureSettings : MonoBehaviour
    {
    public int captureFramerate = 60;
    public float captureTime;

    // Use this for initialization
    void Start ()
    {
    Time.captureFramerate = captureFramerate;
    }

    void Update()
    {
    if (Time.time > captureTime)
    {
    #if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying = false;
    #endif
    Application.Quit();
    }

    Debug.Log(Time.time);
    }
    }