zoukankan      html  css  js  c++  java
  • 关于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 }

    效果图如下:

  • 相关阅读:
    CA证书扫盲,https讲解。
    关于jquery的入门,简单的封装。
    anglar JS使用两层ng-repeat嵌套使用,分辨$index
    JS中=>,>>>是什么意思
    撰写html标签的快捷方式2
    CSS 中伪类的顺序
    撰写html标签的快捷方式1
    文字换行显示
    input 控件监听回车确认按钮。
    git常用命令整理
  • 原文地址:https://www.cnblogs.com/mrmocha/p/9738879.html
Copyright © 2011-2022 走看看