unity 方便的截图保存功能 新建一个摄像机用于截图,深度设为最低
string fileName= Application.streamingAssetsPath + "/photo.jpg";
public void CaptureScreenByRT(Camera camera )
{
Rect rect = new Rect(0, 0, 100, 100);
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
System.IO.File.WriteAllBytes(fileName, bytes);
}
|