关于Unity实现AR功能(三)AR手机截图

 1 /*************************************************
 2  * 项目名称:AR截图
 3  * 脚本创建人:魔卡
 4  * 脚本创建时间:2018.10.02
 5  * 脚本功能:截取当前手机界面图片到系统相册截图中
 6  * ***********************************************/
 7 using System.Collections;
 8 using System.Collections.Generic;
 9 using System.IO;
10 using UnityEngine;
11 using UnityEngine.UI;
12 
13 public class ScreenShot : MonoBehaviour
14 {
15     private Button m_screenShotBtn;//截屏按钮
16 
17     void Awake()
18     {
19         //初始化
20         m_screenShotBtn = transform.Find("ScreenShotBtn").GetComponent<Button>();
21         m_screenShotBtn.onClick.AddListener(OnScreenShotBtnClick);
22     }
23 
24     /// <summary>
25     /// 自定义截屏功能,截屏按钮点击触发
26     /// </summary>
27     public void OnScreenShotBtnClick()
28     {
29 
30 
31         //使用当前时间作为图片名称
32         System.DateTime tNowTime = System.DateTime.Now;
33         string tTime = tNowTime.ToString();
34         //去除两边空格
35         tTime = tTime.Trim();
36         //将“/”用“-”代替
37         tTime = tTime.Replace("/", "-");
38 
39         //存储为png格式的
40         string tFileName = "ARScreenShot" + tTime + ".png";
41 
42         //判断当前运行环境
43         if (Application.platform == RuntimePlatform.Android)
44         {
45             //生成一个Texture2D (参数为:宽,高,纹理,是否使用映射)
46             Texture2D tTexture2D = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
47             //读取Texture2D到本身上
48             tTexture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
49             //图片应用一下
50             tTexture2D.Apply();
51 
52             //将图片转换为二进制进行写入
53             byte[] tBytes = tTexture2D.EncodeToPNG();
54 
55             //写入地址
56             //此处注意,写入的地址是当前手机截屏的默认地址,其他地址也可以存储但是在图册中不会显示出来
57             string tDestination = "/sdcard/DCIM/Screenshots";
58 
59             //判断当前文件夹是否存在,不存在则进行创建
60             if (!Directory.Exists(tDestination))
61             {
62                 Directory.CreateDirectory(tDestination);
63             }
64 
65             //截图存储路径名
66             string tSavePath = tDestination + "/" + tFileName;
67 
68             File.WriteAllBytes(tSavePath, tBytes);
69         }
70     }
71 }

效果图如下:

 

转载于:https://www.cnblogs.com/mrmocha/p/9738879.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值