Last active
July 14, 2020 08:53
-
-
Save atesija/f700fd6679db0e03795b to your computer and use it in GitHub Desktop.
Simple Unity screenshot on timer script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class ScreenshotUtility : MonoBehaviour | |
| { | |
| [Tooltip("The filepath to the save location for the screenshots")] | |
| public string _filepath; | |
| [Tooltip("Scales the image by the amount specified (0 for no scaling)")] | |
| public int _screenshotResolutionScaleFactor = 0; | |
| [Space] | |
| public KeyCode _toggleAutoScreenshots = KeyCode.K; | |
| public float _timeBetweenAutoScreenshots = 3.0f; | |
| [Space] | |
| public KeyCode _pressForSingleScreenshot = KeyCode.L; | |
| private int _screenshotNumber; | |
| private bool _autoScreenshotOn; | |
| void Start() | |
| { | |
| _screenshotNumber = 0; | |
| _autoScreenshotOn = false; | |
| StartCoroutine(AutomaticScreenshot()); | |
| } | |
| void Update() | |
| { | |
| if(Input.GetKeyDown(_toggleAutoScreenshots)) | |
| { | |
| _autoScreenshotOn = !_autoScreenshotOn; | |
| } | |
| if (Input.GetKeyDown(_pressForSingleScreenshot)) | |
| { | |
| TakeScreenshot(); | |
| } | |
| } | |
| IEnumerator AutomaticScreenshot() | |
| { | |
| while (true) | |
| { | |
| if(_autoScreenshotOn) | |
| { | |
| TakeScreenshot(); | |
| yield return new WaitForSeconds(_timeBetweenAutoScreenshots); | |
| } | |
| yield return new WaitForFixedUpdate(); | |
| } | |
| } | |
| //For reference http://docs.unity3d.com/ScriptReference/Application.CaptureScreenshot.html | |
| private void TakeScreenshot() | |
| { | |
| Application.CaptureScreenshot(_filepath + _screenshotNumber++.ToString() + ".png", _screenshotResolutionScaleFactor); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment