public Camera arCamera;
public void OnScreenShotClick()
{
System.DateTime now = System.DateTime.Now;
string times = now.ToString();
times = times.Trim();
times = times.Replace("/", "-");
string fileName = "ARScreenShot" + times + ".png";
if (Application.platform==RuntimePlatform.Android)
{
#region 带ui的截图
////生成截图
//Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
//texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
//texture.Apply();
////保存截图到内存
//byte[] bytes = texture.EncodeToPNG();
//string destination = "/sdcard/DCIM/" + "Screenshots";//前面固定写法,打开手机的存储位置,后面是手机默认的截屏文件夹(可以自定义)
////判断文件是否存在
//if (!Directory.Exists(destination))
//{
// Directory.CreateDirectory(destination);
//}
//string pathSave = destination + "/" + fileName;
////写入内存中的图片
//File.WriteAllBytes(pathSave, bytes);
#endregion
#region 不带ui的截图
RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 1);
arCamera.targetTexture = rt;
arCamera.Render();
RenderTexture.active = rt;
//生成截图
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
//重置
arCamera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
//保存截图到内存
byte[] bytes = texture.EncodeToPNG();
string destination = "/sdcard/DCIM/" + "Screenshots";//前面固定写法,打开手机的存储位置,后面是手机默认的截屏文件夹(可以自定义)
//判断文件是否存在
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
string pathSave = destination + "/" + fileName;
//写入内存中的图片
File.WriteAllBytes(pathSave, bytes);
#endregion
}
}
将上面的方法绑定到你ui的截屏按钮即可,有一点需要注意,这个相机是你想要截屏的图像渲染的相机,比如这里我使用了两个相机,分别渲染场景和ui,物体相机的depth为1,所以实例化RenderTexture的时候最后的参数选为1,但是如果你事先像我这个样子给的固定的相机,这个参数影响不大