CaptureScreenShotExample.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #if UNITY_EDITOR
  2. using System.IO;
  3. using UnityEditor.Recorder;
  4. using UnityEditor.Recorder.Input;
  5. namespace UnityEngine.Recorder.Examples
  6. {
  7. /// <summary>
  8. /// This example shows how to setup a recording session via script.
  9. /// To use this example. Simply add the CaptureScreenShotExample component to a GameObject.
  10. ///
  11. /// Entering playmode will display a "Capture ScreenShot" button.
  12. ///
  13. /// Recorded images are saved in [Project Folder]/SampleRecordings
  14. /// </summary>
  15. public class CaptureScreenShotExample : MonoBehaviour
  16. {
  17. RecorderController m_RecorderController;
  18. void OnEnable()
  19. {
  20. var controllerSettings = ScriptableObject.CreateInstance<RecorderControllerSettings>();
  21. m_RecorderController = new RecorderController(controllerSettings);
  22. var mediaOutputFolder = Path.Combine(Application.dataPath, "..", "SampleRecordings");
  23. // Image
  24. var imageRecorder = ScriptableObject.CreateInstance<ImageRecorderSettings>();
  25. imageRecorder.name = "My Image Recorder";
  26. imageRecorder.Enabled = true;
  27. imageRecorder.OutputFormat = ImageRecorderSettings.ImageRecorderOutputFormat.PNG;
  28. imageRecorder.CaptureAlpha = false;
  29. imageRecorder.OutputFile = Path.Combine(mediaOutputFolder, "image_" + DefaultWildcard.Take);
  30. imageRecorder.imageInputSettings = new GameViewInputSettings
  31. {
  32. OutputWidth = 3840,
  33. OutputHeight = 2160,
  34. };
  35. // Setup Recording
  36. controllerSettings.AddRecorderSettings(imageRecorder);
  37. controllerSettings.SetRecordModeToSingleFrame(0);
  38. }
  39. void OnGUI()
  40. {
  41. if (GUI.Button(new Rect(10, 10, 150, 50), "Capture ScreenShot"))
  42. {
  43. m_RecorderController.PrepareRecording();
  44. m_RecorderController.StartRecording();
  45. }
  46. }
  47. }
  48. }
  49. #endif