当前位置:   article > 正文

Unity 四种截图方法(相机视图、无UI、有UI、Game窗口)_unity 截图

unity 截图

之前介绍了三种截图方法(全屏截图、自定义大小截图、无UI截图)

今天介绍一下在Unity 编辑器中进行截图,这种截图的方式是截取Game窗口视角,在编辑器内可以随时使用,而且还可以设置快捷键

 

 脚本如下:由于此脚本继承的是EditorWindow,使用需要放入Editor文件夹内

  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. public class ScreenCaptureEditor : EditorWindow
  5. {
  6. private static string directory = "Screenshots/Capture/";
  7. private static string latestScreenshotPath = "";
  8. private bool initDone = false;
  9. private GUIStyle BigText;
  10. void InitStyles()
  11. {
  12. initDone = true;
  13. BigText = new GUIStyle(GUI.skin.label)
  14. {
  15. fontSize = 20,
  16. fontStyle = FontStyle.Bold
  17. };
  18. }
  19. private void OnGUI()
  20. {
  21. if (!initDone)
  22. {
  23. InitStyles();
  24. }
  25. GUILayout.Label("Screen Capture Tools", BigText);
  26. if (GUILayout.Button("单张截图"))
  27. {
  28. TakeScreenshot();
  29. }
  30. GUILayout.Label("当前分辨率: " + GetResolution());
  31. if (GUILayout.Button("打开文件夹"))
  32. {
  33. ShowFolder();
  34. }
  35. GUILayout.Label("保存路径: " + directory);
  36. }
  37. [MenuItem("Tools/Screenshots/打开窗口 &`", false, 0)]
  38. public static void ShowWindow()
  39. {
  40. GetWindow(typeof(ScreenCaptureEditor));
  41. }
  42. [MenuItem("Tools/Screenshots/存储路径 &2", false, 2)]
  43. private static void ShowFolder()
  44. {
  45. if (File.Exists(latestScreenshotPath))
  46. {
  47. EditorUtility.RevealInFinder(latestScreenshotPath);
  48. return;
  49. }
  50. Directory.CreateDirectory(directory);
  51. EditorUtility.RevealInFinder(directory);
  52. }
  53. [MenuItem("Tools/Screenshots/单张截图 &1", false, 1)]
  54. private static void TakeScreenshot()
  55. {
  56. Directory.CreateDirectory(directory);
  57. var currentTime = System.DateTime.Now;
  58. var filename = currentTime.ToString().Replace('/', '-').Replace(':', '_') + ".png";
  59. var path = directory + filename;
  60. ScreenCapture.CaptureScreenshot(path);
  61. latestScreenshotPath = path;
  62. Debug.Log($"截图路径: <b>{path}</b> 分辨率: <b>{GetResolution()}</b>");
  63. }
  64. private static string GetResolution()
  65. {
  66. Vector2 size = Handles.GetMainGameViewSize();
  67. Vector2Int sizeInt = new Vector2Int((int)size.x, (int)size.y);
  68. return $"{sizeInt.x}x{sizeInt.y}";
  69. }
  70. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/492286
推荐阅读
相关标签
  

闽ICP备14008679号