Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Camera))]
  6. public class EditorScreenshot : MonoBehaviour
  7. {
  8. [SerializeField] string folder = "EditorScreenshot/results";
  9. [SerializeField] string prefix = "screenshot";
  10. [SerializeField] bool ensureTransparentBackground = false;
  11.  
  12. Camera Camera
  13. {
  14. get
  15. {
  16. if (camera == null) {
  17. camera = GetComponent<Camera>();
  18. }
  19. return camera;
  20. }
  21. }
  22. new Camera camera;
  23. int Width
  24. {
  25. get => Camera.main.pixelWidth;
  26. }
  27. int Height
  28. {
  29. get => Camera.main.pixelHeight;
  30. }
  31.  
  32. [ContextMenu("Take Screenshot")]
  33. public void TakeScreenshot()
  34. {
  35. PrepareCamera();
  36. WriteScreenshot();
  37. }
  38.  
  39. void WriteScreenshot()
  40. {
  41. RenderTexture renderTextureBefore = RenderTexture.active;
  42. RenderTexture.active = camera.targetTexture;
  43.  
  44. Debug.Log($"{Width}x{Height}");
  45.  
  46. Texture2D screenshot = new Texture2D(Width, Height, TextureFormat.ARGB32, false);
  47. screenshot.ReadPixels(new Rect(0, 0, Width, Height), 0, 0, false);
  48.  
  49. if (QualitySettings.activeColorSpace == ColorSpace.Linear)
  50. {
  51. Color[] pixels = screenshot.GetPixels();
  52. for (int p = 0; p < pixels.Length; p++)
  53. {
  54. pixels[p] = pixels[p].gamma;
  55. }
  56. screenshot.SetPixels(pixels);
  57. }
  58. screenshot.Apply(false);
  59.  
  60. folder = GetSafePath(folder.Trim('/'));
  61. prefix = GetSafeFilename(prefix);
  62.  
  63. string dir = $"{Application.dataPath}/{folder}/";
  64. string filename = $"{prefix}_{DateTime.Now.ToString("yyMMdd_HHmmss")}.png";
  65. string path = $"{dir}{filename}";
  66.  
  67. Directory.CreateDirectory(dir);
  68. File.WriteAllBytes(path, screenshot.EncodeToPNG());
  69.  
  70. camera.targetTexture = null;
  71. RenderTexture.active = renderTextureBefore;
  72.  
  73. Debug.Log("Screenshot saved to:\n" + path);
  74. }
  75.  
  76. void PrepareCamera()
  77. {
  78. Camera camera = Camera;
  79. camera.targetTexture = new RenderTexture(Width, Height, 0, RenderTextureFormat.ARGB32);
  80.  
  81. CameraClearFlags clearFlagsBefore = camera.clearFlags;
  82. Color backgroundColorBefore = camera.backgroundColor;
  83.  
  84. if (ensureTransparentBackground)
  85. {
  86. camera.clearFlags = CameraClearFlags.SolidColor;
  87. camera.backgroundColor = new Color();
  88. }
  89.  
  90. camera.Render();
  91.  
  92. if (ensureTransparentBackground)
  93. {
  94. camera.clearFlags = clearFlagsBefore;
  95. camera.backgroundColor = backgroundColorBefore;
  96. }
  97. }
  98.  
  99. public string GetSafePath(string path)
  100. {
  101. return string.Join("_", path.Split(Path.GetInvalidPathChars()));
  102. }
  103.  
  104. public string GetSafeFilename(string filename)
  105. {
  106. return string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement