Unity安卓截屏保存本地相册 您所在的位置:网站首页 怎么在电脑上截图视频并保存到相册 Unity安卓截屏保存本地相册

Unity安卓截屏保存本地相册

2024-07-13 07:37| 来源: 网络整理| 查看: 265

Unity安卓截屏保存本地相册 原创

诗远__ 2021-08-27 09:21:35 博主文章分类:C# ©著作权

文章标签 android java 安卓手机 2d #if 文章分类 代码人生

©著作权归作者所有:来自51CTO博客作者诗远__的原创作品,请联系作者获取转载授权,否则将追究法律责任

 

using UnityEngine; using System.Collections; using System; using System.IO; using UnityEngine.UI; /// /// 截图保存安卓手机相册 /// public class CaptureScreenshotMgr : MonoBehaviour { public Text text; string _name = ""; /// /// 保存截屏图片,并且刷新相册 Android /// /// 若空就按照时间命名 public void CaptureScreenshot() { _name = ""; _name = "Screenshot_" + GetCurTime() + ".png"; #if UNITY_STANDALONE_WIN //PC平台 // 编辑器下 // string path = Application.persistentDataPath + "/" + _name; string path = Application.dataPath + "/" + _name; ScreenCapture.CaptureScreenshot(path, 0); Debug.Log("图片保存地址" + path); #elif UNITY_ANDROID //安卓平台 //Android版本 StartCoroutine(CutImage(_name)); //在手机上显示路径 // text.text = "图片保存地址" + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/DCIM/Camera/" + _name; text.text = "图片保存地址" + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/截屏/" + _name; #endif } //截屏并保存 IEnumerator CutImage(string name) { //图片大小 Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true); yield return new WaitForEndOfFrame(); tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true); tex.Apply(); yield return tex; byte[] byt = tex.EncodeToPNG(); string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")); // File.WriteAllBytes(path + "/DCIM/Camera/" + name, byt); //保存到 安卓手机的 DCIM/下的Camera 文件夹下 File.WriteAllBytes(path + "/截屏/" + name, byt); //保存到安卓手机的 文件管理下面的 《截屏》文件夹下 string[] paths = new string[1]; paths[0] = path; ScanFile(paths); } //刷新图片,显示到相册中 void ScanFile(string[] path) { using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject playerActivity = PlayerActivity.GetStatic("currentActivity"); using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null)) { Conn.CallStatic("scanFile", playerActivity, path, null, null); } } } /// /// 获取当前年月日时分秒,如20181001444 /// /// string GetCurTime() { return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); } }

Unity安卓截屏保存本地相册_2d

 

打包到安卓手机,写入的可能会有些慢,过段时间刷新一下就有了

收藏 评论 分享 举报

上一篇:Unity用键盘控制物体左右旋转前后移动的c#脚本

下一篇:Unity安卓端 复制到沙河路径文件



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有