unity之安卓手机截屏并显示在相册

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using UnityEngine.UI;

public class ScreenShot : MonoBehaviour {
    //截图的相机
    public Camera camera1;

    public Image screenBg;

    //截图缩小图
    Sprite sprites;
    /// <summary>
    /// 截图的按钮事件
    /// </summary>
    public void CameraScreenShot()
    {
        //调用截图方法
        CaptureCamera(camera1, new Rect(0,0,Screen.width,Screen.height));
        //调用给小截图添加图片
        AddImageToScreenKuang();
    }
    Texture2D CaptureCamera(Camera camera, Rect rect)
    {
        //获取系统时间并命名相片名    
        DateTime now = DateTime.Now;
        string times = now.ToString();
        times = times.Trim();
        times = times.Replace(":", "");
        times = times.Replace("/", "");
        times = times.Remove(8,1);
        string filename = "/Screenshot" + times + ".png";

        // 创建一个RenderTexture对象  
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 16);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机  
        camera.targetTexture = rt;
        camera.Render();
        // 激活这个rt, 并从中中读取像素。  
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素  
        screenShot.Apply();


        // 重置相关参数,以使用camera继续在屏幕上显示  
        camera.targetTexture = null;
        //ps: camera2.targetTexture = null;  
        RenderTexture.active = null; // JC: added to avoid errors  
        GameObject.Destroy(rt);
        // 最后将这些纹理数据,成一个png图片文件  
        byte[] bytes = screenShot.EncodeToPNG();
#if UNITY_EDITOR
        string Path_save = Application.streamingAssetsPath + filename;
        sprites = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0.5f, 0.5f));
#elif UNITY_ANDROID
        string destination = "/storage/emulated/0/DCIM/Camera";  
        //判断目录是否存在,不存在则会创建目录    
        if (!Directory.Exists (destination)) {    
            Directory.CreateDirectory (destination);    
        }    
        string Path_save = destination+"/" + filename;  
        sprites = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0.5f, 0.5f));
#elif UNITY_IOS

#endif
        System.IO.File.WriteAllBytes(Path_save, bytes);
        Debug.Log(string.Format("截屏了一张照片: {0}", Path_save));

        return screenShot;
    }
    //添加截图缩小图
    void AddImageToScreenKuang()
    {
        screenBg.sprite = sprites;
        screenBg.gameObject.SetActive(true);
    }
    public void CancelClick()
    {
        screenBg.gameObject.SetActive(false);
    }
}

                
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值