2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

5 years ago
  1. using System.IO;
  2. using Unity.FPS.Game;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace Unity.FPS.UI
  9. {
  10. public class TakeScreenshot : MonoBehaviour
  11. {
  12. [Tooltip("Root of the screenshot panel in the menu")]
  13. public GameObject ScreenshotPanel;
  14. [Tooltip("Name for the screenshot file")]
  15. public string FileName = "Screenshot";
  16. [Tooltip("Image to display the screenshot in")]
  17. public RawImage PreviewImage;
  18. CanvasGroup m_MenuCanvas = null;
  19. Texture2D m_Texture;
  20. bool m_TakeScreenshot;
  21. bool m_ScreenshotTaken;
  22. bool m_IsFeatureDisable;
  23. string GetPath() => k_ScreenshotPath + FileName + ".png";
  24. const string k_ScreenshotPath = "Assets/";
  25. void Awake()
  26. {
  27. #if !UNITY_EDITOR
  28. // this feature is available only in the editor
  29. ScreenshotPanel.SetActive(false);
  30. m_IsFeatureDisable = true;
  31. #else
  32. m_IsFeatureDisable = false;
  33. var gameMenuManager = GetComponent<InGameMenuManager>();
  34. DebugUtility.HandleErrorIfNullGetComponent<InGameMenuManager, TakeScreenshot>(gameMenuManager, this,
  35. gameObject);
  36. m_MenuCanvas = gameMenuManager.MenuRoot.GetComponent<CanvasGroup>();
  37. DebugUtility.HandleErrorIfNullGetComponent<CanvasGroup, TakeScreenshot>(m_MenuCanvas, this,
  38. gameMenuManager.MenuRoot.gameObject);
  39. LoadScreenshot();
  40. #endif
  41. }
  42. void Update()
  43. {
  44. PreviewImage.enabled = PreviewImage.texture != null;
  45. if (m_IsFeatureDisable)
  46. return;
  47. if (m_TakeScreenshot)
  48. {
  49. m_MenuCanvas.alpha = 0;
  50. ScreenCapture.CaptureScreenshot(GetPath());
  51. m_TakeScreenshot = false;
  52. m_ScreenshotTaken = true;
  53. return;
  54. }
  55. if (m_ScreenshotTaken)
  56. {
  57. LoadScreenshot();
  58. #if UNITY_EDITOR
  59. AssetDatabase.Refresh();
  60. #endif
  61. m_MenuCanvas.alpha = 1;
  62. m_ScreenshotTaken = false;
  63. }
  64. }
  65. public void OnTakeScreenshotButtonPressed()
  66. {
  67. m_TakeScreenshot = true;
  68. }
  69. void LoadScreenshot()
  70. {
  71. if (File.Exists(GetPath()))
  72. {
  73. var bytes = File.ReadAllBytes(GetPath());
  74. m_Texture = new Texture2D(2, 2);
  75. m_Texture.LoadImage(bytes);
  76. m_Texture.Apply();
  77. PreviewImage.texture = m_Texture;
  78. }
  79. }
  80. }
  81. }