我想在Unity中使用其他相机拍摄屏幕截图。我已经阅读了一些示例项目和watched this video.
现在,我可以获取屏幕快照并将其保存在使用Unity Editor时的某个位置。但是,当我将其构建到我的Android设备中时,图像变得充满杂讯。
这是我的代码:
void Awake()
{
myCamera = GetComponent<Camera>(); // Camera is disable in the scene
myCamera.targetTexture = new RenderTexture(Screen.width, Screen.height, 24);
}
public void takeScreenShot(){
myCamera.enabled = true;
StartCoroutine(Capture());
}
IEnumerator Capture(){
yield return new WaitForEndOfFrame();
Texture2D result = new Texture2D(myCamera.targetTexture.width, myCamera.targetTexture.height, TextureFormat.RGB24, false);
myCamera.Render();
RenderTexture.active = myCamera.targetTexture;
Rect rect = new Rect(0, 0, myCamera.targetTexture.width, myCamera.targetTexture.height);
result.ReadPixels(rect, 0, 0);
byte[] byteArray = result.EncodeToPNG();
string filePath = Application.persistentDataPath + "/ScreenShot.png";
System.IO.File.WriteAllBytes(filePath, byteArray);
Debug.Log("ScreenShot saved to: " + filePath);
Destroy(result);
myCamera.enabled = false;
}
答案 0 :(得分:0)
最后,我发现这里出了什么问题。我的相机的“清除标志”设置为“不清除”。将清除标志更改为“仅深度”可以解决此问题。