当前位置:网站首页>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();
}
}
边栏推荐
- It is safer for individuals to choose which securities company to open an account for buying floor funds
- 买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
- Industry analysis - quick intercom, building intercom
- setInterval、setTimeout和requestAnimationFrame
- 李宏毅《机器学习》丨7. Conclusion(总结)
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
- 实体转JSON时,值为null的字段的丢失问题
- day39 原型鏈及頁面烟花效果 2021.10.13
- 时间戳和date转换「建议收藏」
- Blackmail virus of industrial control security
猜你喜欢

无法重新声明块范围变量

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

JS foundation 3

Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush

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

String & heap & method area

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

Blue Bridge Cup Maze (dfs+ backtracking)

ThreadLocal的简单理解

Day39 prototype chain and page fireworks effect 2021.10.13
随机推荐
setInterval、setTimeout和requestAnimationFrame
使用 Calendar 计算时间
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
数据库系列:有什么办法对数据库的业务表进行无缝升级
2022中国信通院首届业务与应用安全发展论坛成功召开!
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
[function suggestion] select a space when multiple workspaces are started
Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
JS foundation 4
Compareto() and equals() methods of BigDecimal class
Secretary of the Ukrainian national security and National Defense Commission: will carry out precision strikes against targets in Russia
100 important knowledge points that SQL must master: retrieving data
Day39 prototype chain and page fireworks effect 2021.10.13
MySQL installation configuration and solving the problem of forgetting root password when reinstalling MySQL
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
day39 原型鏈及頁面烟花效果 2021.10.13
静态库的制作和使用
JS基础1-JS引入与运算符
Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
基于验证码识别的机器学习项目captcha_trainer操作实践