Unity3D save ScreenShots by 3 methods
来源:互联网 发布:朋友陪你醉dj网络歌手 编辑:程序博客网 时间:2024/06/18 02:14
/// <summary> /// 使用Application类下的CaptureScreenshot()方法实现截图 /// 优点:简单,可以快速地截取某一帧的画面、全屏截图 /// 缺点:不能针对摄像机截图,无法进行局部截图 /// </summary> /// <param name="mFileName">M file name.</param> private void CaptureByUnity(string mFileName) { Application.CaptureScreenshot(mFileName,0); } /// <summary> /// 根据一个Rect类型来截取指定范围的屏幕 /// 左下角为(0,0) /// </summary> /// <param name="mRect">M rect.</param> /// <param name="mFileName">M file name.</param> private IEnumerator CaptureByRect(Rect mRect,string mFileName) { //等待渲染线程结束 yield return new WaitForEndOfFrame(); //初始化Texture2D Texture2D mTexture=new Texture2D((int)mRect.width,(int)mRect.height,TextureFormat.RGB24,false); //读取屏幕像素信息并存储为纹理数据 mTexture.ReadPixels(mRect,0,0); //应用 mTexture.Apply(); //将图片信息编码为字节信息 byte[] bytes = mTexture.EncodeToPNG(); //保存 System.IO.File.WriteAllBytes(mFileName, bytes); //如果需要可以返回截图 //return mTexture; } private IEnumerator CaptureByCamera(Camera mCamera,Rect mRect,string mFileName) { //等待渲染线程结束 yield return new WaitForEndOfFrame(); //初始化RenderTexture RenderTexture mRender=new RenderTexture((int)mRect.width,(int)mRect.height,0); //设置相机的渲染目标 mCamera.targetTexture=mRender; //开始渲染 mCamera.Render(); //激活渲染贴图读取信息 RenderTexture.active=mRender; Texture2D mTexture=new Texture2D((int)mRect.width,(int)mRect.height,TextureFormat.RGB24,false); //读取屏幕像素信息并存储为纹理数据 mTexture.ReadPixels(mRect,0,0); //应用 mTexture.Apply(); //释放相机,销毁渲染贴图 mCamera.targetTexture = null; RenderTexture.active = null; GameObject.Destroy(mRender); //将图片信息编码为字节信息 byte[] bytes = mTexture.EncodeToPNG(); //保存 System.IO.File.WriteAllBytes(mFileName,bytes); //如果需要可以返回截图 //return mTexture; } }
//定义图片保存路径 private string mPath1; private string mPath2; private string mPath3; //相机 public Transform CameraTrans; void Start() { //初始化路径 mPath1=Application.dataPath+"\\ScreenShot\\ScreenShot1.png"; mPath2=Application.dataPath+"\\ScreenShot\\ScreenShot2.png"; mPath3=Application.dataPath+"\\ScreenShot\\ScreenShot3.png"; } //主方法,使用UGUI实现 void OnGUI() { if(GUILayout.Button("截图方式1",GUILayout.Height(30))){ CaptureByUnity(mPath1); } if(GUILayout.Button("截图方式2",GUILayout.Height(30))){ StartCoroutine(CaptureByRect(new Rect(0,0,1024,768),mPath2)); } if(GUILayout.Button("截图方式3",GUILayout.Height(30))){ //启用顶视图相机 CameraTrans.camera.enabled=true; //禁用主相机 Camera.main.enabled=false; StartCoroutine(CaptureByCamera(CameraTrans.camera,new Rect(0,0,1024,768),mPath3)); } }
阅读全文
0 0
- Unity3D save ScreenShots by 3 methods
- Image segmentation by MCMC methods
- Fedora7 Screenshots
- mac _ Screenshots and screenshots
- 8.7.3 Methods
- 17.5.3 Virtual methods
- 17.5.3 Virtual methods
- 4.4.3 Framework Methods
- 3 Decomposition Methods
- METHODS
- Methods
- Methods
- Unity3d的C#扩展方法Extension methods应用吧
- Some screenshots of Fbx2Cal
- debian的screenshots
- cetia4 1.1文档-2.9 Separating Methods by Request Type
- Getting things in Magento by getModel and getData methods
- asm [transform class by adding the getter methods]
- Linux下PHP安装扩展
- Android之Application引用方式
- Canvas画太极图
- 【超牛】利用Excel制作身份证查询系统!
- Java中的static关键字解析
- Unity3D save ScreenShots by 3 methods
- 项目启动找不到资源文件可能的解决办法
- java后端程序员1年工作经验总结
- Linux
- recipe for target 'doc/cpp.info' failed
- sql server 清空表数据
- JavaScript数组去重
- 全国第一家FPGA云主机(FAAS)正式启动售卖,被阿里云抢先了。
- Java事务处理的基本问题