Advertisement
Guest User

HiResScreenShots.cs

a guest
Dec 19th, 2015
1,464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4.  
  5. public class HiResScreenShots : MonoBehaviour
  6. {
  7.  
  8.     public int resWidth = 1920; public int resHeight = 1080;
  9.  
  10.     [Range(1, 6)]
  11.     public int enlarge = 1;
  12.  
  13.     public bool transparent = false;
  14.  
  15.     public KeyCode screenshotKey = KeyCode.F8;
  16.     private bool takeHiResShot = false;
  17.     private TextureFormat transp = TextureFormat.ARGB32;
  18.     private TextureFormat nonTransp = TextureFormat.RGB24;
  19.  
  20.  
  21.     public static string ScreenShotName(int width, int height)
  22.     {
  23.         return string.Format("{0}/../screenshots/screen_{1}x{2}_{3}.png",
  24.                              Application.dataPath,
  25.                              width, height,
  26.                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
  27.     }
  28.  
  29.     public void TakeHiResShot()
  30.     {
  31.         takeHiResShot = true;
  32.     }
  33.  
  34.  
  35.     void LateUpdate()
  36.     {
  37.         takeHiResShot |= Input.GetKeyDown(KeyCode.F8);
  38.         if (takeHiResShot)
  39.         {
  40.             #if !UNITY_4_3
  41.             #if !UNITY_WEBPLAYER
  42.                 TextureFormat textForm = nonTransp;
  43.                 if (transparent)
  44.                     textForm = transp;
  45.                 RenderTexture rt = new RenderTexture(resWidth * enlarge, resHeight * enlarge, 24);
  46.                 Camera.main.targetTexture = rt;
  47.                 Texture2D screenShot = new Texture2D(resWidth * enlarge, resHeight * enlarge, textForm, false);
  48.                 Camera.main.Render();
  49.                 RenderTexture.active = rt;
  50.                 screenShot.ReadPixels(new Rect(0, 0, resWidth * enlarge, resHeight * enlarge), 0, 0);
  51.                 Camera.main.targetTexture = null;
  52.                 RenderTexture.active = null; // JC: added to avoid errors
  53.                 Destroy(rt);
  54.                 byte[] bytes = screenShot.EncodeToPNG();
  55.                 string filename = ScreenShotName(resWidth * enlarge, resHeight * enlarge);
  56.                 if (Directory.Exists(Application.dataPath + "/../screenshots/") == false)
  57.                 {
  58.                     Directory.CreateDirectory(Application.dataPath + "/../screenshots/");
  59.                 }
  60.                 System.IO.File.WriteAllBytes(filename, bytes);
  61.                 Debug.Log(string.Format("Took screenshot to: {0}", filename));
  62.             #endif
  63.             #endif
  64.  
  65.             takeHiResShot = false;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement