当前位置:网站首页>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();
}
}
边栏推荐
- 方法重写(Override)
- day39 原型链及页面烟花效果 2021.10.13
- 拼接String集合中的字符串_基于Stream
- Graduation season, some suggestions for you who are new to the society
- How to distinguish and define DQL, DML, DDL and DCL in SQL
- Remote connection of raspberry pie in VNC viewer mode without display
- [function suggestion] select a space when multiple workspaces are started
- JS foundation 1-js introduction and operator
- js中this的默认指向及如何修改指向 2021.11.09
- Lihongyi, machine learning 7 Conclusion
猜你喜欢

Making and using of static library

QML control type: tabbar

Oracle 日期格式化异常:无效数字

零基础自学SQL课程 | IF函数

Day39 prototype chain and page fireworks effect 2021.10.13

Making and using of dynamic library (shared library)

获取系统当前日期

JS基础8

论文阅读 (59):Keyword-Based Diverse Image Retrieval with Variational Multiple Instance Graph

Industry analysis - quick intercom, building intercom
随机推荐
JS基础2
Summary of spatial temporal time series prediction modeling methods
关于Pytorch中双向LSTM的输出表示问题
[function suggestion] select a space when multiple workspaces are started
Introduction to GDB
QML control type: tabbar
选择哪种编程语言,会吸引优秀的人才?
Graduated
MySQL installation configuration and solving the problem of forgetting root password when reinstalling MySQL
Redis6 1: what problems can be solved by the introduction of NoSQL and redis?
day33 js笔记 事件(下)2021.09.28
soapui的菜鸟教程
获取系统当前日期
分析list中有无重复数据且重复了几次
第2章 还记得点、线、面吗(二)
Xshell and xftp tutorial
什么是主链系统?
File的io流与base64
day37 js笔记 运动函数 2021.10.11
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击