Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ScreenShot : MonoBehaviour {
  6.     public Camera camera;
  7.     public int resWidth = 2550;
  8.     public int resHeight = 3300;
  9.     private string screensPath;
  10.  
  11.     // Use this for initialization
  12.     void Start () {
  13.        
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.        
  19.     }
  20.     public void TakeHiResShot()
  21.     {
  22.             if (screensPath == null)
  23.             {
  24.                 #if UNITY_ANDROID && !UNITY_EDITOR
  25.                 screensPath = "/sdcard/DCIM/RegionCapture";
  26.  
  27.                 #elif UNITY_IPHONE && !UNITY_EDITOR
  28.                 screensPath = Application.persistentDataPath;
  29.  
  30.                 #else
  31.                 screensPath = Application.dataPath + "/Screens";
  32.  
  33.                 #endif
  34.                 System.IO.Directory.CreateDirectory(screensPath);
  35.             }
  36.             RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
  37.             camera.targetTexture = rt;
  38.             Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
  39.             camera.Render();
  40.             RenderTexture.active = rt;
  41.             screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
  42.             camera.targetTexture = null;
  43.             RenderTexture.active = null; // JC: added to avoid errors
  44.             Destroy(rt);
  45.             byte[] bytes = screenShot.EncodeToPNG();
  46.             string fileName = screensPath + "/screen_" + System.DateTime.Now.ToString("dd_MM_HH_mm_ss") + ".png";
  47.             System.IO.File.WriteAllBytes(fileName, bytes);
  48.             Debug.Log(string.Format("Took screenshot to: {0}", fileName));
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement