Capture and save screenshot with ScreenCapture.CaptureScreenshot

前端 未结 3 582
感动是毒
感动是毒 2021-01-15 19:14

I\'ve been trying to take a screenshot and then immediately after, use it to show some sort of preview and some times it works and some times it doesn\'t, I\'m currently not

3条回答
  •  被撕碎了的回忆
    2021-01-15 19:39

    Programmer's code worked successfully by being called like the following. It is designed as coroutine, so it would not interfere frame rate. Hence it should be called as coroutine. Make sure the CallerObject is inheriting from "MonoBehaviour".

     public class CallerObject : MonoBehaviour
    {
        public void Caller()
        {
            String imagePath = Application.persistentDataPath + "/image.png";
            StartCoroutine(captureScreenshot(imagePath));
        }
    
    
        IEnumerator captureScreenshot(String imagePath)
        {
            yield return new WaitForEndOfFrame();
            //about to save an image capture
            Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
    
    
            //Get Image from screen
            screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
            screenImage.Apply();
    
            Debug.Log(" screenImage.width" + screenImage.width + " texelSize" + screenImage.texelSize);
            //Convert to png
            byte[] imageBytes = screenImage.EncodeToPNG();
    
            Debug.Log("imagesBytes=" + imageBytes.Length);
    
            //Save image to file
            System.IO.File.WriteAllBytes(imagePath, imageBytes);
        }
    }
    

提交回复
热议问题