When I run on my Android tablet and touch the button, the button is disappeared from the screen, so it seems to success.
However, in the screenshot image which is obtained by the script, the button is also captured.
I was working on something similar and stumbled upon this thread. There’s a slight flicker on the UI when capturing the screenshot, but it kinda works for me. Hope it helps
public void OnClickScreenCaptureButton()
{
StartCoroutine(CaptureScreen());
}
public IEnumerator CaptureScreen()
{
// Wait till the last possible moment before screen rendering to hide the UI
yield return null;
GameObject.Find("MyCanvas").GetComponent<Canvas>().enabled = false;
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
// Take screenshot
Application.CaptureScreenshot("screenshot.png");
// Show UI after we're done
GameObject.Find("MyCanvas").GetComponent<Canvas>().enabled = true;
}
Application.CaptureScreenshot() api have become redundant, see the documentation
below code by @pixpusher2 works with slight changes.
// Take screenshot
//Application.CaptureScreenshot("screenshot.png");
ScreenCapture.CaptureScreenshot(Application.persistentDataPath+"/screenshot.png");//use this
The button would not turn off until the end of the frame. You are then taking a picture of that frame which has the UI enabled. What I would suggest is putting a simple delay after the button is pressed. Then you can take a screen shot.
The code by @pixpusher2 works perfectly along with the suggestion from @monstrXR . But the problem I am facing is that it is only saving one screenshot per runtime. How can I save multiple screenshot per runtime on click? This is the code i am using now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System.IO;
public class ScreenShot : MonoBehaviour
{
string ScreenShotfilename = string.Format("Screenshot-{0:yyyy-MM-dd_hh-mm-ss-tt}.png", System.DateTime.Now);
public void OnClickScreenCaptureButton()
{
StartCoroutine(CaptureScreen());
StopCoroutine(CaptureScreen());
}
public IEnumerator CaptureScreen()
{
// Wait till the last possible moment before screen rendering to hide the UI
yield return null;
GameObject.Find("Canvas").GetComponent<Canvas>().enabled = false;
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
// Take screenshot
//Application.CaptureScreenshot("screenshot.png");
ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/"+ ScreenShotfilename); //use this
// Show UI after we're done
GameObject.Find("Canvas").GetComponent<Canvas>().enabled = true;
Debug.Log("ScreenShot");
Debug.Log(Application.persistentDataPath);
}
}