vitorfgd

ScreenShot Modified

Jan 2nd, 2016
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4.  
  5. public class HighResSS : MonoBehaviour {
  6.  
  7.     public bool TakeScreenShot_iOS = false, iOSPortrait = false, TakeScreenShotAndroid = false, androidPortrait = false;
  8.     private int [] iOSRes = new int[] {960, 640, 1136, 640, 1334, 750, 2208, 1242, 2048, 1536, 2732, 2048};
  9.     private int picture = 0;
  10.  
  11.     [Tooltip("Android does not have specific size (Min. Size: 320px; Max. Size: 3840px.)")]
  12.     public int android_width, android_height;
  13.  
  14.     [Range(1, 6)]
  15.     public int enlarge = 1;
  16.    
  17.     public bool transparent = false;
  18.    
  19.     public KeyCode screenshotKey = KeyCode.F8;
  20.     private bool takeHiResShot = false;
  21.     private TextureFormat transp = TextureFormat.ARGB32;
  22.     private TextureFormat nonTransp = TextureFormat.RGB24;
  23.     private string size;
  24.  
  25.     public static string ScreenShotName(int photoNumber, string plataform, int width, int height){
  26.         return string.Format("{0}/../screenshots/" + photoNumber + "_" + plataform + "screen_{1}x{2}_{3}.png",
  27.                              Application.dataPath,
  28.                              width, height,
  29.                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
  30.     }
  31.    
  32.     public void TakeHiResShot(){;
  33.         takeHiResShot = true;
  34.     }
  35.  
  36.     void LateUpdate(){
  37.         takeHiResShot |= Input.GetKeyDown(KeyCode.F8);
  38.         if (takeHiResShot){
  39.             picture ++;
  40.             if (TakeScreenShot_iOS && !TakeScreenShotAndroid){
  41.                 if (!iOSPortrait)
  42.                     LandScapeiOS ();
  43.                 else if (iOSPortrait)
  44.                     PortraitiOS ();
  45.             }
  46.  
  47.             else if (!TakeScreenShot_iOS && TakeScreenShotAndroid){
  48.                 ScreenShotAndroid ();
  49.             }
  50.  
  51.             else if (TakeScreenShot_iOS && TakeScreenShotAndroid){
  52.                 if (!iOSPortrait)
  53.                     LandScapeiOS ();
  54.                 else if (iOSPortrait)
  55.                     PortraitiOS ();
  56.                 ScreenShotAndroid();
  57.             }
  58.             takeHiResShot = false;
  59.         }
  60.     }
  61.  
  62.     public void LandScapeiOS (){
  63.         for (int i = 0; i < iOSRes.Length; i += 2){
  64.             TextureFormat textForm = nonTransp;
  65.  
  66.             if (transparent)
  67.                 textForm = transp;
  68.  
  69.             if (i == 0)
  70.                 size = "3.5";
  71.             else if (i == 2)
  72.                 size = "4";
  73.             else if (i == 4)
  74.                 size = "4.7";
  75.             else if (i == 6)
  76.                 size = "5.5";
  77.             else if (i == 8)
  78.                 size = "iPad";
  79.             else if (i == 10)
  80.                 size = "iPadPro";
  81.  
  82.             RenderTexture rt = new RenderTexture(iOSRes[i] * enlarge, iOSRes[i+1] * enlarge, 24);
  83.             Camera.main.targetTexture = rt;
  84.             Texture2D screenShot = new Texture2D(iOSRes[i] * enlarge, iOSRes[i+1] * enlarge, textForm, false);
  85.             Camera.main.Render();
  86.             RenderTexture.active = rt;
  87.             screenShot.ReadPixels(new Rect(0, 0, iOSRes[i] * enlarge, iOSRes[i+1] * enlarge), 0, 0);
  88.             Camera.main.targetTexture = null;
  89.             RenderTexture.active = null;
  90.             Destroy(rt);
  91.             byte[] bytes = screenShot.EncodeToPNG();
  92.             string filename = ScreenShotName(picture, "IOS_" + size + "_LANDSCAPE+", iOSRes[i] * enlarge, iOSRes[i+1] * enlarge);
  93.            
  94.             if (Directory.Exists(Application.dataPath + "/../screenshots/") == false)
  95.                 Directory.CreateDirectory(Application.dataPath + "/../screenshots/");
  96.            
  97.             System.IO.File.WriteAllBytes(filename, bytes);
  98.             Debug.Log(string.Format("Took screenshot to: {0}", filename));
  99.         }
  100.     }
  101.  
  102.     public void PortraitiOS (){
  103.         for (int i = 0; i < iOSRes.Length; i += 2){
  104.             TextureFormat textForm = nonTransp;
  105.  
  106.             if (transparent)
  107.                 textForm = transp;
  108.  
  109.             if (i == 0)
  110.                 size = "3.5";
  111.             else if (i == 2)
  112.                 size = "4";
  113.             else if (i == 4)
  114.                 size = "4.7";
  115.             else if (i == 6)
  116.                 size = "5.5";
  117.             else if (i == 8)
  118.                 size = "iPad";
  119.             else if (i == 10)
  120.                 size = "iPadPro";
  121.  
  122.             RenderTexture rt = new RenderTexture(iOSRes[i+1] * enlarge, iOSRes[i] * enlarge, 24);
  123.             Camera.main.targetTexture = rt;
  124.             Texture2D screenShot = new Texture2D(iOSRes[i+1] * enlarge, iOSRes[i] * enlarge, textForm, false);
  125.             Camera.main.Render();
  126.             RenderTexture.active = rt;
  127.             screenShot.ReadPixels(new Rect(0, 0, iOSRes[i+1] * enlarge, iOSRes[i] * enlarge), 0, 0);
  128.             Camera.main.targetTexture = null;
  129.             RenderTexture.active = null;
  130.             Destroy(rt);
  131.             byte[] bytes = screenShot.EncodeToPNG();
  132.             string filename = ScreenShotName(picture, "IOS_" + size + "_PORTRAIT+", iOSRes[i+1] * enlarge, iOSRes[i] * enlarge);
  133.            
  134.             if (Directory.Exists(Application.dataPath + "/../screenshots/") == false)
  135.                 Directory.CreateDirectory(Application.dataPath + "/../screenshots/");
  136.            
  137.             System.IO.File.WriteAllBytes(filename, bytes);
  138.             Debug.Log(string.Format("Took screenshot to: {0}", filename));
  139.         }
  140.     }
  141.  
  142.     public void ScreenShotAndroid (){
  143.  
  144.         if (android_width == 0) {
  145.             if (!androidPortrait)
  146.                 android_width = 1600;
  147.             else
  148.                 android_width = 900;
  149.         }
  150.  
  151.         if (android_height == 0) {
  152.             if (!androidPortrait)
  153.                 android_height = 900;
  154.             else
  155.                 android_height = 1600;
  156.         }
  157.  
  158.         TextureFormat textForm = nonTransp;
  159.         if (transparent)
  160.             textForm = transp;
  161.         RenderTexture rt = new RenderTexture(android_width * enlarge, android_height * enlarge, 24);
  162.         Camera.main.targetTexture = rt;
  163.         Texture2D screenShot = new Texture2D(android_width * enlarge, android_height * enlarge, textForm, false);
  164.         Camera.main.Render();
  165.         RenderTexture.active = rt;
  166.         screenShot.ReadPixels(new Rect(0, 0, android_width * enlarge, android_height * enlarge), 0, 0);
  167.         Camera.main.targetTexture = null;
  168.         RenderTexture.active = null;
  169.         Destroy(rt);
  170.         byte[] bytes = screenShot.EncodeToPNG();
  171.         string filename = ScreenShotName(picture, "ANDROID+", android_width * enlarge, android_height * enlarge);
  172.  
  173.         if (Directory.Exists(Application.dataPath + "/../screenshots/") == false)
  174.             Directory.CreateDirectory(Application.dataPath + "/../screenshots/");
  175.  
  176.         System.IO.File.WriteAllBytes(filename, bytes);
  177.         Debug.Log(string.Format("Took screenshot to: {0}", filename));
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment