当前位置:网站首页>Unity screenshot function
Unity screenshot function
2022-06-28 11:29:00 【Xiao Chen Xue game】
One : Screen capture of mobile terminal
1: Open the project settings first , Set the selected part in the following figure to be consistent with the picture .
2: Create the following script . Just call CaptureScreenshot Method to enable the screenshot function .
among bool Variable IsUi It is used to control whether the screenshot is taken with UI Elements .
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using UnityEngine.UI;
/// <summary>
/// Screenshot save Android phone photo album
/// </summary>
public class CaptureScreenshotMgr1 : MonoBehaviour
{
public bool isUi=false;// Controls whether the screenshot content contains UI
public Toggle isUiToggle;// Test use You can choose to keep or delete
private void Update()
{
isUi = isUiToggle.isOn;
}
public Text text;
string _name = "";
/// <summary>
/// Save the screenshot , And refresh the album Android
/// </summary>
/// <param name="name"> If it is empty, name it according to time </param>
public void CaptureScreenshot()
{
_name = "";
_name = "Screenshot_" + GetCurTime() + ".png";
#if UNITY_STANDALONE_WIN //PC platform
// Under the editor
// string path = Application.persistentDataPath + "/" + _name;
string path = Application.dataPath + "/" + _name;
ScreenCapture.CaptureScreenshot(path, 0);
Debug.Log(" Image storage address " + path);
#elif UNITY_ANDROID // Android platform
//Android edition
if (isUi)
{
StartCoroutine(CutImage1(_name));
}
else
{
StartCoroutine(CutImage(_name));
}
// Show the path on the phone
// text.text = " Image storage address " + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/DCIM/Camera/" + _name;
text.text = " Image storage address " + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/ screenshots /" + _name;
#endif
}
// Take a screenshot and save No UI
IEnumerator CutImage(string name)
{
// Picture size
//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);// Read pixel
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); // Save to Android phones DCIM/ Under the Camera Under the folder
//File.WriteAllBytes(path + "/ screenshots /" + name, byt); // Saved to Android phone File management 《 screenshots 》 Under the folder
string[] paths = new string[1];
paths[0] = path;
ScanFile(paths);
}
// Capture and save belt UI
IEnumerator CutImage1(string name)
{
// Picture size
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);// Read pixel
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); // Save to Android phones DCIM/ Under the Camera Under the folder
//File.WriteAllBytes(path + "/ screenshots /" + name, byt); // Saved to Android phone File management 《 screenshots 》 Under the folder
string[] paths = new string[1];
paths[0] = path;
ScanFile(paths);
}
// Refresh the picture , Show in album
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>
/// Get the current day, month, hour, minute, second , Such as 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();
}
}
边栏推荐
- Compareto() and equals() methods of BigDecimal class
- JS基础4
- 买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
- windows 10下载安装mysql5.7
- 2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
- [sciter]: use of sciter FS module scanning file API and its details
- vsftpd服务的部署及优化
- js中this的默认指向及如何修改指向 2021.11.09
- 一套十万级TPS的IM综合消息系统的架构实践与思考
- 论文阅读 (59):Keyword-Based Diverse Image Retrieval with Variational Multiple Instance Graph
猜你喜欢

Word、PDF、TXT文件实现全文内容检索需要用什么方法?

如临现场的视觉感染力,NBA决赛直播还能这样看?

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)

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

JS基础8

Characteristics of solar wireless LED display

Fancy features and cheap prices! What is the true strength of Changan's new SUV?

day32 js笔记 事件(上)2021.09.27

一套十万级TPS的IM综合消息系统的架构实践与思考
随机推荐
Graduation season, some suggestions for you who are new to the society
Create ECS using API shortcut
BigDecimal 类的 compareTo() 和 equals()方法
Secretary of the Ukrainian national security and National Defense Commission: will carry out precision strikes against targets in Russia
JS foundation 2
Docker modifies the user name and password of MySQL
[cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
SQL必需掌握的100个重要知识点:检索数据
AGCO AI frontier promotion (6.28)
Thesis reading (59):keyword based diverse image retrieval with variable multiple instance graph
一套十万级TPS的IM综合消息系统的架构实践与思考
JS基础4
2022中国信通院首届业务与应用安全发展论坛成功召开!
Zero foundation self-study SQL course | if function
MySQL cannot query the maximum value using the max function
js中this的默认指向及如何修改指向 2021.11.09
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
windows 10下载安装mysql5.7
Xshell和Xftp使用教程