ScreenShotTester.cs 994 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. //游戏全屏截图-快捷截图
  6. public class ScreenShotTester : MonoBehaviour
  7. {
  8. private string saveDir;
  9. public static void Init(string saveDir = "C:\\Users\\JC\\Desktop\\弓箭截图\\")
  10. {
  11. if (!saveDir.EndsWith("\\")) throw new System.Exception("路径错误");
  12. if (FindObjectOfType<ScreenShotTester>()) return;
  13. var comp = new GameObject(typeof(ScreenShotTester).Name).AddComponent<ScreenShotTester>();
  14. comp.saveDir = saveDir;
  15. DontDestroyOnLoad(comp);
  16. }
  17. void Update()
  18. {
  19. #if UNITY_EDITOR
  20. if (Input.GetKeyDown(KeyCode.S))
  21. {
  22. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  23. string fileName = ts.TotalMilliseconds + ".jpg";
  24. ScreenCapture.CaptureScreenshot(saveDir + fileName);
  25. Debug.LogWarning("截图成功-" + fileName);
  26. }
  27. #endif
  28. }
  29. }