ScreenshotTaker.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //C# Example
  2. using UnityEditor;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. public class Screenshot : EditorWindow
  6. {
  7. int resWidth = Screen.width*4;
  8. int resHeight = Screen.height*4;
  9. public Camera myCamera;
  10. int scale = 1;
  11. string path = "";
  12. bool showPreview = true;
  13. RenderTexture renderTexture;
  14. bool isTransparent = false;
  15. // Add menu item named "My Window" to the Window menu
  16. [MenuItem("Tools/Saad Khawaja/Instant High-Res Screenshot")]
  17. public static void ShowWindow()
  18. {
  19. //Show existing window instance. If one doesn't exist, make one.
  20. EditorWindow editorWindow = EditorWindow.GetWindow(typeof(Screenshot));
  21. editorWindow.autoRepaintOnSceneChange = true;
  22. editorWindow.Show();
  23. editorWindow.title = "Screenshot";
  24. }
  25. float lastTime;
  26. void OnGUI()
  27. {
  28. EditorGUILayout.LabelField ("Resolution", EditorStyles.boldLabel);
  29. resWidth = EditorGUILayout.IntField ("Width", resWidth);
  30. resHeight = EditorGUILayout.IntField ("Height", resHeight);
  31. EditorGUILayout.Space();
  32. scale = EditorGUILayout.IntSlider ("Scale", scale, 1, 15);
  33. EditorGUILayout.HelpBox("The default mode of screenshot is crop - so choose a proper width and height. The scale is a factor " +
  34. "to multiply or enlarge the renders without loosing quality.",MessageType.None);
  35. EditorGUILayout.Space();
  36. GUILayout.Label ("Save Path", EditorStyles.boldLabel);
  37. EditorGUILayout.BeginHorizontal();
  38. EditorGUILayout.TextField(path,GUILayout.ExpandWidth(false));
  39. if(GUILayout.Button("Browse",GUILayout.ExpandWidth(false)))
  40. path = EditorUtility.SaveFolderPanel("Path to Save Images",path,Application.dataPath);
  41. EditorGUILayout.EndHorizontal();
  42. EditorGUILayout.HelpBox("Choose the folder in which to save the screenshots ",MessageType.None);
  43. EditorGUILayout.Space();
  44. //isTransparent = EditorGUILayout.Toggle(isTransparent,"Transparent Background");
  45. GUILayout.Label ("Select Camera", EditorStyles.boldLabel);
  46. myCamera = EditorGUILayout.ObjectField(myCamera, typeof(Camera), true,null) as Camera;
  47. if(myCamera == null)
  48. {
  49. myCamera = Camera.main;
  50. }
  51. isTransparent = EditorGUILayout.Toggle("Transparent Background", isTransparent);
  52. EditorGUILayout.HelpBox("Choose the camera of which to capture the render. You can make the background transparent using the transparency option.",MessageType.None);
  53. EditorGUILayout.Space();
  54. EditorGUILayout.BeginVertical();
  55. EditorGUILayout.LabelField ("Default Options", EditorStyles.boldLabel);
  56. if(GUILayout.Button("Set To Screen Size"))
  57. {
  58. resHeight = (int)Handles.GetMainGameViewSize().y;
  59. resWidth = (int)Handles.GetMainGameViewSize().x;
  60. }
  61. if(GUILayout.Button("Default Size"))
  62. {
  63. resHeight = 1440;
  64. resWidth = 2560;
  65. scale = 1;
  66. }
  67. EditorGUILayout.EndVertical();
  68. EditorGUILayout.Space();
  69. EditorGUILayout.LabelField ("Screenshot will be taken at " + resWidth*scale + " x " + resHeight*scale + " px", EditorStyles.boldLabel);
  70. if(GUILayout.Button("Take Screenshot",GUILayout.MinHeight(60)))
  71. {
  72. if(path == "")
  73. {
  74. path = EditorUtility.SaveFolderPanel("Path to Save Images",path,Application.dataPath);
  75. Debug.Log("Path Set");
  76. TakeHiResShot();
  77. }
  78. else
  79. {
  80. TakeHiResShot();
  81. }
  82. }
  83. EditorGUILayout.Space();
  84. EditorGUILayout.BeginHorizontal();
  85. if(GUILayout.Button("Open Last Screenshot",GUILayout.MaxWidth(160),GUILayout.MinHeight(40)))
  86. {
  87. if(lastScreenshot != "")
  88. {
  89. Application.OpenURL("file://" + lastScreenshot);
  90. Debug.Log("Opening File " + lastScreenshot);
  91. }
  92. }
  93. if(GUILayout.Button("Open Folder",GUILayout.MaxWidth(100),GUILayout.MinHeight(40)))
  94. {
  95. Application.OpenURL("file://" + path);
  96. }
  97. if(GUILayout.Button("More Assets",GUILayout.MaxWidth(100),GUILayout.MinHeight(40)))
  98. {
  99. Application.OpenURL("https://www.assetstore.unity3d.com/en/#!/publisher/5951");
  100. }
  101. EditorGUILayout.EndHorizontal();
  102. if (takeHiResShot)
  103. {
  104. int resWidthN = resWidth*scale;
  105. int resHeightN = resHeight*scale;
  106. RenderTexture rt = new RenderTexture(resWidthN, resHeightN, 24);
  107. myCamera.targetTexture = rt;
  108. TextureFormat tFormat;
  109. if(isTransparent)
  110. tFormat = TextureFormat.ARGB32;
  111. else
  112. tFormat = TextureFormat.RGB24;
  113. Texture2D screenShot = new Texture2D(resWidthN, resHeightN, tFormat,false);
  114. myCamera.Render();
  115. RenderTexture.active = rt;
  116. screenShot.ReadPixels(new Rect(0, 0, resWidthN, resHeightN), 0, 0);
  117. myCamera.targetTexture = null;
  118. RenderTexture.active = null;
  119. byte[] bytes = screenShot.EncodeToPNG();
  120. string filename = ScreenShotName(resWidthN, resHeightN);
  121. System.IO.File.WriteAllBytes(filename, bytes);
  122. Debug.Log(string.Format("Took screenshot to: {0}", filename));
  123. Application.OpenURL(filename);
  124. takeHiResShot = false;
  125. }
  126. EditorGUILayout.HelpBox("In case of any error, make sure you have Unity Pro as the plugin requires Unity Pro to work.",MessageType.Info);
  127. }
  128. private bool takeHiResShot = false;
  129. public string lastScreenshot = "";
  130. public string ScreenShotName(int width, int height) {
  131. string strPath="";
  132. strPath = string.Format("{0}/screen_{1}x{2}_{3}.png",
  133. path,
  134. width, height,
  135. System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
  136. lastScreenshot = strPath;
  137. return strPath;
  138. }
  139. public void TakeHiResShot() {
  140. Debug.Log("Taking Screenshot");
  141. takeHiResShot = true;
  142. }
  143. }