ScreenshotToolEditor.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. public static class ScreenshotToolEditor
  5. {
  6. private const string PREF_KEY_LAST_PATH = "ScreenshotTool_LastPath";
  7. [MenuItem("Tools/截图/捕获当前屏幕 %#s")] // Ctrl/Cmd + Shift + S
  8. public static void CaptureScreenshot()
  9. {
  10. // 从 EditorPrefs 读取上次保存的路径
  11. string lastPath = EditorPrefs.GetString(PREF_KEY_LAST_PATH, Application.dataPath);
  12. // 弹出保存对话框
  13. string path = EditorUtility.SaveFilePanel(
  14. "选择截图保存位置",
  15. lastPath, // 👈 打开上次使用的文件夹
  16. "Screenshot",
  17. "png"
  18. );
  19. if (string.IsNullOrEmpty(path))
  20. {
  21. Debug.Log("📸 截图已取消。");
  22. return;
  23. }
  24. // 保存路径下次复用
  25. string directory = Path.GetDirectoryName(path);
  26. EditorPrefs.SetString(PREF_KEY_LAST_PATH, directory);
  27. // 执行截图
  28. ScreenCapture.CaptureScreenshot(path, 1);
  29. Debug.Log($"✅ 截图已保存:{path}");
  30. }
  31. }