using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ScreenShot : MonoBehaviour
{
private Camera arCamera;
void Start()
{
arCamera = GameObject.Find("ARCamera").GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
}
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)
{
//包含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 pataSave = destination + "/" + fileName;
//File.WriteAllBytes(pataSave, bytes);
//不包含UI的方式
RenderTexture renderTexture=new RenderTexture(Screen.width, Screen.height,1);
arCamera.targetTexture = renderTexture;
arCamera.Render();
RenderTexture.active = renderTexture;
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(renderTexture);
byte[] bytes = texture.EncodeToPNG();
string destination = "/sdcard/DCIM/ScreenShots";
if (!Directory.Exists((destination)))
{
Directory.CreateDirectory(destination);
}
string pataSave = destination + "/" + fileName;
File.WriteAllBytes(pataSave, bytes);
}
}
}