ScreenShot.cs 6.3 KB

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