[Edited to provide additional info/partial answer at bottom]
Any constructive ideas on how to approach de-bugging this problem are appreciated.
I followed this tutorial on taking and displaying a screenshot using Unity's Screencapture.Capturescreenshot() method. https://www.youtube.com/watch?v=DQeylS0l4S4&t=92s
On display, my screenshot image (a basic cube in 3D space) appears translucent. Does anyone have any idea why and how I can fix it so it shows a true screen shot? I used another phone to take a picture of my actual screen when I'm taking the screen shot here (Sorry it's a little blurry; the cube is moving):
And a picture of the displayed screen shot. Notice how translucent it is:
This is code to capture the screen image:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TakeScreenshot : MonoBehaviour {
public void TakeAShot()
{
StartCoroutine ("CaptureIt");
}
IEnumerator CaptureIt()
{
string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
string fileName = "Screenshot" + timeStamp + ".png";
string pathToSave = fileName;
ScreenCapture.CaptureScreenshot(pathToSave);
yield return new WaitForEndOfFrame();
}
}
And here is the code for displaying. It converts the .png file to a texture, makes a sprite out of the texture, and displays the sprite on the canvas. My canvas is set to default parameters in the inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class ScreenshotPreview : MonoBehaviour {
public GameObject canvas;
string[] files = null;
int whichScreenShotIsShown= 0;
// Use this for initialization
void Start () {
files = Directory.GetFiles(Application.persistentDataPath + "/", "*.png");
if (files.Length > 0) {
GetPictureAndShowIt ();
}
}
void GetPictureAndShowIt()
{
string pathToFile = files [whichScreenShotIsShown];
Texture2D texture = GetScreenshotImage (pathToFile);
Sprite sp = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height),
new Vector2 (0.5f, 0.5f));
canvas.GetComponent<Image> ().sprite = sp;
}
Texture2D GetScreenshotImage(string filePath)
{
Texture2D texture = null;
byte[] fileBytes;
if (File.Exists (filePath)) {
fileBytes = File.ReadAllBytes (filePath);
texture = new Texture2D (2, 2, TextureFormat.RGB24, false);
texture.LoadImage (fileBytes);
}
return texture;
}
}
Additional information:
So my thinking was that the problem had to either be in the capturing or the displaying. I realized I could look at the saved .png file in my Android files to see what they looked like before they are pulled back into the app for display.
The result? : The .png looks perfect. So it's just a problem with the displaying. I still haven't solved the whole problem but now I know the screen is being captured perfectly with Unity's Screencapture.Capturescreenshot method.