当前位置:网站首页>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();
}
}
边栏推荐
- 工作组环境下的内网渗透:一些基础打法
- 论文阅读 (59):Keyword-Based Diverse Image Retrieval with Variational Multiple Instance Graph
- JS基础8
- Day39 prototype chain and page Fireworks Effect 2021.10.13
- Calculate time using calendar
- JS基础5
- Docker modifies the user name and password of MySQL
- Splicing strings in the string collection_ Stream based
- js中的class类模式及语法 2021.11.10
- It is safer for individuals to choose which securities company to open an account for buying floor funds
猜你喜欢

JS foundation 3

String & heap & method area

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?

ThreadLocal的简单理解

Wealth management for programmers

Redis6 1: what problems can be solved by the introduction of NoSQL and redis?

New listing of operation light 3.0 - a sincere work of self subversion across the times!

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

字符串 & 堆 & 方法区

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
随机推荐
day36 js笔记 ECMA6语法 2021.10.09
合约量化交易系统开发 | 合约量化APP开发(现成案例)
动态库(共享库)的制作和使用
New listing of operation light 3.0 - a sincere work of self subversion across the times!
【SemiDrive源码分析】【X9芯片启动流程】32 - DisPlay模块分析 - RTOS侧
QML control type: tabbar
Zero foundation self-study SQL course | if function
soapui的菜鸟教程
Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
String & heap & method area
Basic 02: variable, remember the mobile number of the object
Lihongyi, machine learning 7 Conclusion
Which broker is safer and more convenient to open an account for Oriental Fortune mobile stock?
Get current system date
元宇宙系统的发展与原理介绍
MySql5.7添加新用户
阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
JS foundation 4
Setinterval, setTimeout and requestanimationframe
毕业了