问题描述
[已编辑以在底部提供其他信息/部分答案]
赞赏任何有关如何调试此问题的建设性想法。
我按照本教程使用Unity的Screencapture.Capturescreenshot()方法获取并显示屏幕截图。 https://www.youtube.com/watch?v=DQeylS0l4S4&t=92s
在显示时,我的屏幕截图图像(3D空间中的基本立方体)看起来是半透明的。有谁知道为什么以及如何解决它,以便显示真实的屏幕截图?当我在此处拍摄屏幕快照时,我用另一部手机拍摄了我的实际屏幕照片(对不起,有点模糊;立方体在移动):
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();
}
}
这是显示代码。它将.png文件转换为纹理,从该纹理中制作出精灵,然后将其显示在画布上。我的画布在检查器中设置为默认参数。
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,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;
}
}
其他信息:
所以我的想法是问题必须出在捕捉或显示上。我意识到我可以查看保存在Android文件中的.png文件,以查看它们的外观,然后再将其拉回应用程序进行显示。
结果? :.png看起来很完美。因此,这只是显示的问题。我仍然没有解决整个问题,但是现在我知道使用Unity的Screencapture.Capturescreenshot方法可以完美地捕获屏幕。
解决方法
事实证明,第二个场景中我的面板的“ alpha”值约为50%,alpha值约为127。要在屏幕上准确显示图像,需要将alpha设置为在255处,表示透明度为0%。通常,许多人希望UI元素具有一定程度的透明性,以便您可以看到UI元素后面的主要场景。但是,对于查看屏幕截图,我希望没有透明度。因此=>在层次结构中选择面板。转到检查器中的“颜色质量”。双击然后将Alpha值设置为255。
如果您的场景中没有专用于查看屏幕截图的专用场景,并且希望在单个场景中更改alpha值,则可以使用脚本进行更改。就我而言,我可以将Alpha永久设置为255,因为该场景的唯一目的(实际上是该面板的唯一目的)是显示屏幕截图。