Unity3D Android截屏 保存到手机相册

//带UI截屏

public void OnScreenShotClick() {
DateTime now = DateTime.Now;
string times = now.ToString();
times = times.Trim();
times = times.Replace("/","-");
string fileName = "ARScreenShot" + times + ".png";
if (Application.platform==RuntimePlatform.Android)
{

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 path = "/sdcard/Pictures/Screenshots";

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filePath = path + "/" + fileName;
File.WriteAllBytes(filePath, bytes);

}

//不带UI截屏

扫描二维码关注公众号,回复: 8216844 查看本文章

 

 

public void OnScreenShotClick() {
DateTime now = DateTime.Now;
string times = now.ToString();
times = times.Trim();
times = times.Replace("/","-");
string fileName = "ARScreenShot" + times + ".png";
if (Application.platform==RuntimePlatform.Android)
{

RenderTexture tr = new RenderTexture(Screen.width,Screen.height,1);
arCamera.targetTexture = tr;
arCamera.Render();
RenderTexture.active = tr;

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(tr);

byte[] bytes = texture.EncodeToPNG();
string path = "/sdcard/Pictures/Screenshots";

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filePath = path + "/" + fileName;
File.WriteAllBytes(filePath, bytes);
}

猜你喜欢

转载自www.cnblogs.com/Damon-3707/p/12053076.html