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截屏

 

 

 

 

 

 

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);
}

posted @ 2019-12-17 10:33  C#初学者—Damon  阅读(837)  评论(0编辑  收藏  举报