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

Class pattern and syntax in JS 2021.11.10

TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush

Blue Bridge Cup Maze (dfs+ backtracking)

还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!

一套十万级TPS的IM综合消息系统的架构实践与思考

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022

阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别

JS基础3

无法重新声明块范围变量

《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
随机推荐
GCC简介
JS foundation 5
JS基础1-JS引入与运算符
Xshell和Xftp使用教程
Blackmail virus of industrial control security
Adding a new user in MySQL 5.7
day34 js笔记 正则表达式 2021.09.29
太阳能无线LED显示屏的特点
网页提示此站点不安全解决方案
Oracle 日期格式化异常:无效数字
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
JS foundation 6
MySQL cannot query the maximum value using the max function
近况
Remote connection of raspberry pie in VNC viewer mode without display
Web page tips this site is unsafe solution
GEE:基于 MCD64A1 的 GlobFire 日常火灾数据集
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
MySQL installation configuration and solving the problem of forgetting root password when reinstalling MySQL