当前位置:网站首页>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();
}
}
边栏推荐
- 静态库的制作和使用
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- [sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
- This Exception was thrown from a job compiled with Burst, which has limited exception support. report errors
- Docker modifies the user name and password of MySQL
- 一套十万级TPS的IM综合消息系统的架构实践与思考
- Lihongyi, machine learning 7 Conclusion
- Industry analysis - quick intercom, building intercom
- 董宇辉,新东方以及凤凰卫视
- 李宏毅《机器学习》丨7. Conclusion(总结)
猜你喜欢

无法重新声明块范围变量

Summary of spatial temporal time series prediction modeling methods

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

Web page tips this site is unsafe solution

Recommended practice sharing of Zhilian recruitment based on Nebula graph
This Exception was thrown from a job compiled with Burst, which has limited exception support. report errors

获取系统当前日期

day29 js笔记 2021.09.23

day37 js笔记 运动函数 2021.10.11

Day39 prototype chain and page Fireworks Effect 2021.10.13
随机推荐
Which broker is safer and more convenient to open an account for Oriental Fortune mobile stock?
Splicing strings in the string collection_ Stream based
MytipartFile与File的相互转换
Basic 02: variable, remember the mobile number of the object
如临现场的视觉感染力,NBA决赛直播还能这样看?
李宏毅《机器学习》丨7. Conclusion(总结)
零基础自学SQL课程 | IF函数
Xshell and xftp tutorial
Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
day33 js笔记 事件(下)2021.09.28
Introduction to GDB
Oracle 日期格式化异常:无效数字
Convert the file URL in the browser to a file stream
JS基础6
阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
Create ECS using API shortcut
NFT卡牌链游系统开发dapp搭建技术详情
Characteristics of solar wireless LED display
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush