untiy之屏幕截图各种方式
来源:互联网 发布:中国人口实时数据 编辑:程序博客网 时间:2024/05/21 20:28
方式一:直接使用unity带的截图函数
Application.CaptureScreenshot(“imagename”)
保存路径:
1. 在PC上保存路径为Application.dataPath(项目所在的路径)
2. 在安卓或者Iphone平台上保存路径为Application.persistentDataPath(游戏里保存数据时放的一个持久数据目录)
优点:简单粗暴
缺点:PC、Mac上正常,但是在移动平台上会出现卡顿现象。
方式二:通过屏幕缓存转化为Png图片进行截图。
IEnumerator GetCapture()
{
//等待所有的摄像机跟GUI渲染完成
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
//如果 recalculateMipMaps 设置为真,这个贴图的mipmaps就会更新 如果 recalculateMipMaps设置为假,你需要调用Apply重新计算它们
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, true);
byte[] imagebytes = tex.EncodeToPNG();//转化为png图
tex.Compress(false);//对屏幕缓存进行压缩
System.IO.File.WriteAllBytes(Application.dataPath + "/screencapture" + shotID + ".png", imagebytes);//存储png图
}
方式三:截图特定相机,自定义截图内容
public void ShotThree()
{
RenderTexture renderTex = new RenderTexture(Screen.width,Screen.height,0);
if (shotCamera.targetTexture == null)
{
shotCamera.targetTexture = renderTex;
}
//手动渲染相机。
shotCamera.Render();
//所有的渲染将进入激活的RenderTexture,如果活动的RenderTexture为null,所有的东西都被渲染到主窗口
RenderTexture.active = renderTex;
Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);//
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
shotCamera.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(renderTex);
// 最后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
shotID++;
System.IO.File.WriteAllBytes(shotPath + "/screencapture" + shotID + ".png", bytes);
}
扩展方式四:鼠标画区域截取,实现方式同上,核心思想为记录鼠标按下跟抬起的点,算出矩形,读取该矩形范围内的像素保存为png图片。
手机拍照并保存到手机相册路径
using UnityEngine;
using System.Collections;
using System.IO;
public class takephoto : MonoBehaviour
{
private int i = 0;
//UI
public GameObject[] btn;
//存储路径
private string Path_save;
//读取路径
private string Path_read;
private string filepath;
private string destination;
void Start()
{
filepath = Application.persistentDataPath + "/拍好玩.png";
}
void OnClick()
{
StartCoroutine(getTexture2d());
}
IEnumerator getTexture2d()
{
//隐藏UI
for (int j = 0; j < btn.Length; j++)
{
btn[j].GetComponentInChildren<UISprite>().enabled = false;
}
//截图操作
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24,false);
//显示UI
for (int j = 0; j < btn.Length; j++)
{
btn[j].GetComponentInChildren<UISprite>().enabled = true;
}
t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
byte[] bytes = t.EncodeToPNG();
t.Compress(true);
t.Apply();
//获取系统时间
System.DateTime now = new System.DateTime();
now = System.DateTime.Now;
string filename = string.Format("拍好玩{0}{1}{2}{3}.png", now.Day, now.Hour, now.Minute, now.Second);
//记录每一个截图名字
StreamWriter sw;
FileInfo ft = new FileInfo(filepath);
if (!ft.Exists)
{
sw = ft.CreateText();
}
else
{
sw = ft.AppendText();
}
sw.WriteLine(filename);
sw.Close();
sw.Dispose();
//应用平台判断,路径选择
if (Application.platform == RuntimePlatform.Android)
{
string origin = Path_save;
destination = "/mnt/sdcard/DCIM/拍好玩";
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
destination = destination + "/" + filename;
Path_save = destination;
}
//保存文件
File.WriteAllBytes(Path_save, bytes);
}
}
- untiy之屏幕截图各种方式
- Android 各种屏幕截图方法
- unity之屏幕截图
- Android屏幕截图方式总结
- PHP之屏幕生成截图
- iphone开发之屏幕截图
- linux之设备屏幕截图
- iOS开发之屏幕截图
- MFC之屏幕截图工具
- 屏幕录像工具之屏幕截图
- E72i 屏幕截图宝典之百事屏幕截图v2.0
- Android屏幕截图之View方法
- linux屏幕截图之滚动截屏
- Android屏幕截图之View方法
- Android屏幕截图之View方法
- Watir2.0.1之——屏幕截图
- Android屏幕截图之View方法
- Android源码--Launcher笔记之屏幕截图
- gpio matrix keypad linux driver
- opencv中的vs框架中的Blob Tracking Tests的中文注释。
- 如何能让英语水平在一年半载内得到大幅提升?(备注下)
- firefox浏览器下,A标签单击一次,提交了两次访问请求
- git使用ssh密钥
- untiy之屏幕截图各种方式
- API的设计与实现
- 用户掩码umask
- Linux学习日志--文件搜索命令
- listview加载性能优化及有多种listitem布局处理方式
- SAP在物流工作中的应用之学习笔记
- 用10046 跟踪exp
- IT技能
- 浅谈参加SEO培训是否是必要的