• 关于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 }

    效果图如下:

  • 相关阅读:
    第4章 栈和队列
    第3章 线性表
    第2章 算法
    第1章 数据结构绪论
    First Blood
    第52条:通过接口引用对象
    第51条:当心字符串连接的性能
    第50条:如果其他类型更合适,则尽量避免使用字符串
    第49条:基本类型优先于装箱基本类型
    第48条:如果需要精确的答案,请避免使用float和double
  • 原文地址:https://www.cnblogs.com/mrmocha/p/9738879.html
Copyright © 2020-2023  润新知