当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

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

Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community

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

JS foundation 3

JS foundation 1-js introduction and operator

day32 js笔记 事件(上)2021.09.27

Jetpack Compose Desktop 桌面版本的打包和发布应用
This Exception was thrown from a job compiled with Burst, which has limited exception support. report errors

Class pattern and syntax in JS 2021.11.10

vsftpd服务的部署及优化
随机推荐
什么是DAPP系统发展与解析理解
vsftpd服务的部署及优化
【剑指Offer】49. 丑数
MySql5.7添加新用户
毕业季 新的开始
分析list中有无重复数据且重复了几次
It is safer for individuals to choose which securities company to open an account for buying floor funds
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
零基础自学SQL课程 | IF函数
QML control type: tabbar
获取系统当前日期
JS基础10
MySQL cannot query the maximum value using the max function
[sciter]: use of sciter FS module scanning file API and its details
Zero foundation self-study SQL course | if function
工作组环境下的内网渗透:一些基础打法
Blackmail virus of industrial control security
JS基础2
Xshell and xftp tutorial