unity2022截屏保存到安卓相册

本文介绍了一个Unity脚本,用于在游戏中保存截图到指定路径,并实现了截图的压缩处理及刷新相册的功能。该脚本针对Android平台进行了特别优化,允许用户将游戏内的截图保存到手机相册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class SaveImagesToPlartform : MonoBehaviour
{
    
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        private void SaveImages()
        {
            string path = Application.streamingAssetsPath;
#if UNITY_ANDROID && !UNITY_EDITOR
            path = "/sdcard/DCIM/Camera"; //设置图片保存到设备的目录.
#endif
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            string savePath = path + "/" + "test" + ".png";
            try
            {
                Application.HasUserAuthorization(UserAuthorization.Microphone);
                byte[] data = DeCompress(ScreenCapture.CaptureScreenshotAsTexture()).EncodeToPNG();
                File.WriteAllBytes(savePath, data);
                OnSaveImagesPlartform(savePath);
            }
            catch
            {   
            }
        }
        /// <summary>
        /// 刷新相册(不需要单独创建原生aar或jar)
        /// </summary>
        /// <param name="path"></param>
        private void OnSaveImagesPlartform(string filePath)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            string[] paths = new string[1];
            paths[0] = filePath; 
            using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
                using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
                {
                    Conn.CallStatic("scanFile", playerActivity, paths, null, null);
                }
            }
#endif
        }
        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public Texture2D DeCompress(Texture2D source)
        {
            RenderTexture renderTex = RenderTexture.GetTemporary(
                        source.width,
                        source.height,
                        0,
                        RenderTextureFormat.Default,
                        RenderTextureReadWrite.Linear);

            Graphics.Blit(source, renderTex);
            RenderTexture previous = RenderTexture.active;
            RenderTexture.active = renderTex;
            Texture2D readableText = new Texture2D(source.width, source.height);
            readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
            readableText.Apply();
            RenderTexture.active = previous;
            RenderTexture.ReleaseTemporary(renderTex);
            return readableText;
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值