unity 游戏截屏到相册

唔,直接丢在一个公共的静态类就可以了

QDebug:https://blog.csdn.net/qq_17813937/article/details/79801371

QFileOperation:https://blog.csdn.net/qq_17813937/article/details/81190245

    /// <summary>
    /// 截屏
    /// </summary>
    /// <param name="action"></param>
    public static void CaptureScreenshot(Action action)
    {
        //QCoroutine 用来开协程的对象
        QCoroutine.Start(CaptureScreenshotIEnumerator(action));
    }

    private static IEnumerator CaptureScreenshotIEnumerator(Action action)
    {
        var screenshotPath = Application.persistentDataPath + "/Screenshot.jpg";
        QFileOperation.DeleteFile(screenshotPath);

        ScreenCapture.CaptureScreenshot("Screenshot.jpg");

        yield return new WaitForSeconds(1f);

#if UNITY_ANDROID || UNITY_IPHONE
        //添加到相册
        string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
        path += "DCIM/Camera";
        QFileOperation.CreateDirctory(path);
        
        var filePath = string.Format("{0}/石现科技{1}.jpg", path, QGlobalFun.GetCurTime());
        var texture = QFileOperation.ReadTexture2D(screenshotPath);
        QDebug.Log("读取图片");
        QFileOperation.WriteBytes(filePath, texture.EncodeToJPG());
        QDebug.Log("写入相册 "+filePath);
        ScanFile(new string[] { filePath });
        QDebug.Log("刷新相册");
        GameObject.Destroy(texture);
        QFileOperation.DeleteFile(screenshotPath);
#else

#endif
        action();
    }

 

    /// <summary>
    /// 刷新相册
    /// </summary>
    /// <param name="path"></param>
    public static void ScanFile(string[] path)
    {
        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, path, null, null);
            }
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值