IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> unity 安卓端AR版截屏、保存、调取图片 -> 正文阅读

[游戏开发]unity 安卓端AR版截屏、保存、调取图片

1.主体代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Menu : MonoBehaviour
{
??? //单例模式
??? public static Menu instance;
??? /// <summary>
??? /// 主页界面
??? /// </summary>
??? public GameObject zhuye;
??? /// <summary>
??? /// 模式界面
??? /// </summary>
??? public GameObject moshi;
??? /// <summary>
??? /// 识别界面
??? /// </summary>
??? public GameObject shibie;
??? /// <summary>
??? /// 拍照界面
??? /// </summary>
??? public GameObject paizhao;
??? /// <summary>
??? ///?? 保存界面
??? /// </summary>
??? public GameObject baocun;
??? /// <summary>
??? /// 提示文字
??? /// </summary>
??? public Text tishi;
??? /// <summary>
??? /// 模型系数
??? /// </summary>
??? public int number;
??? /// <summary>
??? /// 照片展示
??? /// </summary>
??? public RawImage image;
??? /// <summary>
??? /// 合影动画
??? /// </summary>
??? public GameObject[] donghua = new GameObject[] { };
??? /// <summary>
??? /// 图片个数
??? /// </summary>
??? private int pictrueNumber = 0;
??? /// <summary>
??? /// 截图
??? /// </summary>
??? private Texture2D screenShot;
??? /// <summary>
??? /// 保存按钮
??? /// </summary>
??? public Button baocunBtn;
??? /// <summary>
??? /// 保存提示板
??? /// </summary>
??? public GameObject baocuntishi;

??? /// <summary>
??? /// 照片存储的地址
??? /// </summary>
??? private string path;

??? /// <summary>
??? /// 照片展示的实例
??? /// </summary>
??? public GameObject pref;
??? /// <summary>
??? /// 实例化的位置
??? /// </summary>
??? public Transform content;
??? /// <summary>
??? /// 图片放大界面
??? /// </summary>
??? public Image da;
??? /// <summary>
??? /// 图片放大展示
??? /// </summary>
??? public RawImage daPictrue;
??? /// <summary>
??? /// 相册展示界面
??? /// </summary>
??? public GameObject ImageLibrary;

??? void Awake()
??? {
??????? instance = this;

??? }


??? void Start()
??? {
??????? zhuyeMenu();
??????? path = ScreenshotPath();
??????? da.gameObject.SetActive(false);
??????? ImageLibrary.SetActive(false);
??? }

??? /// <summary>
??? /// 主页
??? /// </summary>
??? public void zhuyeMenu()
??? {
??????? zhuye.SetActive(true);
??????? moshi.SetActive(false);
??????? shibie.SetActive(false);
??????? paizhao.SetActive(false);
??????? baocun.SetActive(false);
??????? number = 0;
??????? Debug.Log("主页!");
??????? donghuNumber();
??????? baocuntishi.SetActive(false);
??? }

??? /// <summary>
??? /// 模式
??? /// </summary>
??? public void moshiMenu()
??? {
??????? zhuye.SetActive(false);
??????? moshi.SetActive(true);
??????? shibie.SetActive(false);
??????? paizhao.SetActive(false);
??????? baocun.SetActive(false);
??????? number = 0;
??????? donghuNumber();
??? }

??? /// <summary>
??? /// 选择场景动画
??? /// </summary>
??? /// <param name="index">系数</param>
??? public void xuanxze(int index)
??? {
??????? number = index;
??????? zhuye.SetActive(false);
??????? moshi.SetActive(false);
??????? shibie.SetActive(true);
??????? paizhao.SetActive(false);
??????? baocun.SetActive(false);
??????? paizhaoMenu();
??? }

??? /// <summary>
??? /// 拍照模式
??? /// </summary>
??? public void paizhaoMenu()
??? {
??????? zhuye.SetActive(false);
??????? moshi.SetActive(false);
??????? shibie.SetActive(false);
??????? paizhao.SetActive(true);
??????? baocun.SetActive(false);
??????? donghuNumber();
??? }

??? /// <summary>
??? /// 保存模式
??? /// </summary>
??? public void baocunMenu(Texture2D tx)
??? {
??????? zhuye.SetActive(false);
??????? moshi.SetActive(false);
??????? shibie.SetActive(false);
??????? paizhao.SetActive(false);
??????? baocun.SetActive(true);
??????? baocunBtn.interactable = true;
??????? image.texture = tx;
??? }

??? /// <summary>
??? /// 出现哪个物体
??? /// </summary>
??? public void donghuNumber()
??? {

??????? for (int i = 0; i < donghua.Length; i++)
??????? {
??????????? if (donghua[i].activeSelf == true)
??????????? {
??????????????? donghua[i].SetActive(false);
??????????? }
??????? }

??????? if (number > 0)
??????? {
??????????? donghua[number - 1].SetActive(true);
??????? }

??? }

??? /// <summary>
??? /// 获取图片
??? /// </summary>
??? public void GetPictrue()
??? {
??????? paizhao.SetActive(false);
??????? StartCoroutine(CaptureScreen());
??? }
??? public IEnumerator CaptureScreen()
??? {
??????? yield return new WaitForSeconds(0.1f);
??????? yield return new WaitForEndOfFrame();

??????? var rect = new Rect(0, 0, Screen.width, Screen.height);
??????? Texture2D tx = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
??????? tx.ReadPixels(rect, 0, 0);
??????? tx.Apply();
??????? screenShot = tx;
??????? baocunMenu(tx);
??? }

??? /// <summary>
??? /// 保存图片
??? /// </summary>
??? public void SavePictrue()
??? {
??????? baocunBtn.interactable = false;

??????? string dirPath = path;


??????? string names = GetCurTime();
??????? string filename = path + names + ".png";
??????? byte[] bytes = screenShot.EncodeToPNG();
??????? File.WriteAllBytes(filename, bytes);

??????? Debug.Log("保存照片:" + names + ".png");
??????? baocuntishi.SetActive(true);
??????? StartCoroutine(buxian());
??? }

??? /// <summary>
??? /// 重新拍照
??? /// </summary>
??? public void RefreshPictrue()
??? {
??????? paizhaoMenu();
??? }
??? /// <summary>
??? /// 保存提示板不显示
??? /// </summary>
??? /// <returns></returns>
??? IEnumerator buxian()
??? {
??????? yield return new WaitForSeconds(2f);
??????? baocuntishi.SetActive(false);
??? }

??? /// <summary>
??? /// 图片保存路径
??? /// </summary>
??? /// <returns></returns>
??? public string ScreenshotPath()
??? {

??????? string filePath = "";
??????? switch (Application.platform)
??????? {
??????????? case RuntimePlatform.WindowsEditor:
??????????????? filePath = Application.streamingAssetsPath + "/Pictrue/";
??????????????? Debug.Log("模式:" + "RuntimePlatform.WindowsEditor");
??????????????? break;
??????????? case RuntimePlatform.OSXEditor:
??????????????? Debug.Log("模式:" + "RuntimePlatform.OSXEditor");
??????????????? filePath = Application.persistentDataPath + "/Pictrue/";

??????????????? break;
??????????? case RuntimePlatform.Android:
??????????????? Debug.Log("模式:" + "RuntimePlatform.Android");
??????????????? filePath = Application.persistentDataPath + "/Pictrue/";
??????????????? break;
??????????? case RuntimePlatform.IPhonePlayer:
??????????????? Debug.Log("模式:" + "RuntimePlatform.IPhonePlayer");
??????????????? filePath = Application.persistentDataPath + "/Pictrue/";
??????????????? break;
??????? }

??????? if (!Directory.Exists(filePath))
??????? {
??????????? Directory.CreateDirectory(filePath);
??????? }
??????? return filePath;

??? }
? ?
??? /// <summary>
??? /// 获取当前年月日时分秒,如20181001444
??? /// </summary>
??? /// <returns></returns>
??? string GetCurTime()
??? {
??????? return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
??????????? + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
??? }

??? /// <summary>
??? /// 打开文件夹
??? /// </summary>
??? public void OpenFile()
??? {
??????? string output = ScreenshotPath();
??????? if (!Directory.Exists(output))
??????? {
??????????? Directory.CreateDirectory(output);
??????? }
??????? output = output.Replace("/", "\\");
??????? System.Diagnostics.Process.Start("explorer.exe", output);
??? }

??? /// <summary>
??? /// 点击调出相册
??? /// </summary>
??? public void Xiangce()
??? {
??????? ImageLibrary.SetActive(true);
??????? string[] dirs = Directory.GetFiles(path, "*.png");
??????? int i = 0;
??????? while (i < dirs.Length)
??????? {
??????????? Debug.Log(dirs[i]);
??????????? //一定要加"file:///",否则安卓端找不到位置,加载不出来图片
??????????? StartCoroutine(GetTexture("file:///" + dirs[i]));
??????????? i++;
??????? }
??? }
??? /// <summary>
??? /// 异步加载图片
??? /// </summary>
??? /// <param name="file">地址</param>
??? /// <returns></returns>
??? IEnumerator GetTexture(string file)
??? {
??????? //这里的地址可以填本地文件地址? file://[文件路径]
??????? using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(file))
??????? {
??????????? yield return www.SendWebRequest();
??????????? if (www.result == UnityWebRequest.Result.ConnectionError)
??????????? {
??????????????? Debug.Log(www.error);
??????????? }
??????????? else
??????????? {
??????????????? /*这里把贴图转换的过程放到了后台线程来完成,
??????????????? 并且相比于直接脚本加载图片做了优化,减少了内存分配*/
??????????????? Texture2D tx = DownloadHandlerTexture.GetContent(www);
??????????????? RectTransform xin = (Instantiate(pref) as GameObject).GetComponent<RectTransform>();
??????????????? xin.transform.SetParent(content);
??????????????? xin.transform.localScale = new Vector3(1, 1, 1);
??????????????? xin.transform.GetComponent<Pictrue>().xianshi(tx);
??????????????? ContentSize();
??????????? }
??????? }

??? }
??? /// <summary>
??? /// 滑动条的大小设置
??? /// </summary>
??? public void ContentSize()
??? {
??????? int number = content.childCount;
??????? if (number > 12)
??????? {
??????????? int index = (number - 12) / 4;

??????????? if (number % 4 == 0)
??????????? {
??????????????? content.GetComponent<RectTransform>().sizeDelta = new Vector2(1900, 960 + index * 360);
??????????? }
??????????? else
??????????? {
??????????????? content.GetComponent<RectTransform>().sizeDelta = new Vector2(1900, 960 + (index + 1) * 360);
??????????? }

??????? }
??????? else
??????? {
??????????? content.GetComponent<RectTransform>().sizeDelta = new Vector2(1900, 960);
??????? }
??? }

??? /// <summary>
??? /// 图片放大
??? /// </summary>
??? public void FangDa(Texture2D tx)
??? {
??????? daPictrue.texture = tx;
??????? da.gameObject.SetActive(true);
??? }
??? /// <summary>
??? /// 关闭放大
??? /// </summary>
??? public void DaQuit()
??? {
??????? da.gameObject.SetActive(false);
??? }

??? /// <summary>
??? /// 关闭相册
??? /// </summary>
??? public void XiangceClearn()
??? {
??????? ImageLibrary.SetActive(false);
??????? foreach (Transform go in content)
??????? {
??????????? Destroy(go.gameObject);
??????? }
??? }

}

2.实例代码

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

public class Pictrue : MonoBehaviour
{


??? Texture2D myTextrue2d;

??? public RawImage raw;

??? void Start()
??? {
??????? //raw = transform.GetComponent<RawImage>();
??? }

??? public void xianshi(Texture2D tx)
??? {
??????? myTextrue2d = tx;
??????? raw.texture = myTextrue2d;
??? }


??? public void OnDisable()
??? {
??????? Destroy(myTextrue2d);
??? }

??? public void Click()
??? {
??????? Menu.instance.FangDa(myTextrue2d);
??? }


}

?PLAYER SETTING :这个设置一定别忘记了

备注:无论是pc端还是安卓端都好使,亲测!

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-06 10:10:07  更:2021-08-06 10:11:00 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 15:21:38-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码