unity發布安卓 截圖保存到本地


using System.IO;

//獲取系統時間並命名相片名
System.DateTime now = System.DateTime.Now;
string times = now.ToString ();
times = times.Trim ();
times = times.Replace ("/","-");
//文件名
string filename = "Screenshot"+times+".png";
//判斷是否為Android平台
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 destination = "/sdcard/DCIM/ARphoto";
//判斷目錄是否存在,不存在則會創建目錄
if (!Directory.Exists (destination)) {
Directory.CreateDirectory (destination);
}
//文件路徑
string Path_save = destination+"/" + filename;
//存圖片
System.IO.File.WriteAllBytes(Path_save, bytes);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM