当前位置:网站首页>Unity屏幕截图功能
Unity屏幕截图功能
2022-06-28 11:21:00 【小陈学游戏】
一:移动端截屏
1:先打开项目设置,将下图所选中部分设置成图片一致。
2:创建如下脚本。只需要调用CaptureScreenshot方法即可开启截图功能。
其中bool 变量IsUi是用来控制截图是否带UI元素。
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using UnityEngine.UI;
/// <summary>
/// 截图保存安卓手机相册
/// </summary>
public class CaptureScreenshotMgr1 : MonoBehaviour
{
public bool isUi=false;//控制截图内容是否带UI
public Toggle isUiToggle;//测试使用 可自行选择保留或删除
private void Update()
{
isUi = isUiToggle.isOn;
}
public Text text;
string _name = "";
/// <summary>
/// 保存截屏图片,并且刷新相册 Android
/// </summary>
/// <param name="name">若空就按照时间命名</param>
public void CaptureScreenshot()
{
_name = "";
_name = "Screenshot_" + GetCurTime() + ".png";
#if UNITY_STANDALONE_WIN //PC平台
// 编辑器下
// string path = Application.persistentDataPath + "/" + _name;
string path = Application.dataPath + "/" + _name;
ScreenCapture.CaptureScreenshot(path, 0);
Debug.Log("图片保存地址" + path);
#elif UNITY_ANDROID //安卓平台
//Android版本
if (isUi)
{
StartCoroutine(CutImage1(_name));
}
else
{
StartCoroutine(CutImage(_name));
}
//在手机上显示路径
// text.text = "图片保存地址" + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/DCIM/Camera/" + _name;
text.text = "图片保存地址" + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/截屏/" + _name;
#endif
}
//截屏并保存 不带UI
IEnumerator CutImage(string name)
{
//图片大小
//Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
RenderTexture rt = new RenderTexture(Screen.width, Screen.height,0);
yield return new WaitForEndOfFrame();
Camera.main.targetTexture = rt;
Camera.main.Render();
RenderTexture.active = rt;
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);//读像素
tex.Apply();
Camera.main.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
yield return tex;
byte[] byt = tex.EncodeToPNG();
string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
File.WriteAllBytes(path + "/DCIM/Camera/" + name, byt); //保存到 安卓手机的 DCIM/下的Camera 文件夹下
//File.WriteAllBytes(path + "/截屏/" + name, byt); //保存到安卓手机的 文件管理下面的 《截屏》文件夹下
string[] paths = new string[1];
paths[0] = path;
ScanFile(paths);
}
//截图并保存 带UI
IEnumerator CutImage1(string name)
{
//图片大小
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);//读像素
tex.Apply();
yield return tex;
byte[] byt = tex.EncodeToPNG();
string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
File.WriteAllBytes(path + "/DCIM/Camera/" + name, byt); //保存到 安卓手机的 DCIM/下的Camera 文件夹下
//File.WriteAllBytes(path + "/截屏/" + name, byt); //保存到安卓手机的 文件管理下面的 《截屏》文件夹下
string[] paths = new string[1];
paths[0] = path;
ScanFile(paths);
}
//刷新图片,显示到相册中
void ScanFile(string[] path)
{
using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
{
Conn.CallStatic("scanFile", playerActivity, path, null, null);
}
}
}
/// <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();
}
}
边栏推荐
- 太阳能无线LED显示屏的特点
- Yann Lecun's new paper: the road to building automatic agents
- JS基础6
- [cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
- JS基础1-JS引入与运算符
- 静态库的制作和使用
- 什么是主链系统?
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
- 数据库系列:有什么办法对数据库的业务表进行无缝升级
- 智联招聘基于 Nebula Graph 的推荐实践分享
猜你喜欢

String & heap & method area

vsftpd服务的部署及优化

Wealth management for programmers

Yann Lecun's new paper: the road to building automatic agents

Thesis reading (59):keyword based diverse image retrieval with variable multiple instance graph

智联招聘基于 Nebula Graph 的推荐实践分享

day34 js笔记 正则表达式 2021.09.29

AGCO AI frontier promotion (6.28)

Docker modifies the user name and password of MySQL

科研丨Web of Science检索技巧
随机推荐
毕业了
MytipartFile与File的相互转换
Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
工作组环境下的内网渗透:一些基础打法
ThreadLocal的简单理解
js中的class类模式及语法 2021.11.10
Recommended practice sharing of Zhilian recruitment based on Nebula graph
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
day36 js笔记 ECMA6语法 2021.10.09
将浏览器中的文件 url转换为File流
元宇宙系统的发展与原理介绍
关于Pytorch中双向LSTM的输出表示问题
时间戳和date转换「建议收藏」
Mysql使用max函数查询不到最大值
Setinterval, setTimeout and requestanimationframe
Thesis reading (59):keyword based diverse image retrieval with variable multiple instance graph
JS基础6
静态库的制作和使用
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
Wireshark数据抓包分析之FTP协议