IEnumerator GifCapture() { List <Bitmap> _bmps = new List <Bitmap>(); float duration = 0f; _captureActive = true; while ((duration < maxCaptureSeconds) && _captureActive) { duration += Time.deltaTime; //wait for graphics to render yield return(new WaitForEndOfFrame()); // create a _texture to pass to encoding _texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //put buffer into _texture _texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); _texture.Apply(); //split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy yield return(0); byte[] bytes = _texture.EncodeToPNG(); _bmps.Add(new Bitmap(new MemoryStream(bytes))); yield return(new WaitForSeconds(1f / millisecondsPerFrame)); } _captureActive = false; if (duration >= maxCaptureSeconds) { CaptureEndMaxText(); } else { CaptureEndNormalText(); } string timestamp = System.DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss"); string filePath = Application.dataPath + "/../Assets/GameToGif/ScreenCapture/" + fileName + "_" + timestamp; ConvertToGif.Convert(filePath, millisecondsPerFrame, _bmps); UnityEditor.AssetDatabase.Refresh(); }
//listen for keyboard button press void Update() { //you can change selected keyboard button for different favorite button if (Input.GetKeyDown("s")) { StartCoroutine(TinyCapture()); } if (Input.GetKeyDown("v")) { _captureActive = !_captureActive; _frameCount = 0; _time = 1.0f / captureFrequency; if (_captureActive) { _bmps = new List <Bitmap>(); } else { string timestamp = System.DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss"); string filePath = Application.dataPath + "/../Assets/TinyScreenCapture/ScreenCapture/" + fileName + "_" + timestamp; ConvertToGif.Convert(filePath, captureFrequency, _bmps); } } if (_captureActive) { _time += Time.deltaTime; if (_time >= 1.0f / captureFrequency) { _time = 0f; StartCoroutine(TinyCaptureClip()); } } UpdateCursor(); }