unity中正交unity第一人称摄像机机截图因注意什么

一、截图是经常用到的功能,下面是可能遇到的需求:
1.全屏截图
2.局部截图
3.针对某一相机的截图
4.去除UI元素截图
二、截图可以考虑的方法:
1. Application.CaptureScreenshot(“Screenshot.png”, 0);
截取某一帧时整个游戏的画面,全屏截图。
参数1是图片名称,需加后缀.png;参数二是是否压缩,0不压缩,小于1时压缩,大于1是扩大。
保存路径:工程根目录,editor环境在asset下;android环境在应用程序沙盒中。
实现简单,在任何地方来这么一句就好;
不能针对某一个相机(camera)的画面进行截图。
对局部画面截图,实现起来不方便,效率也低
2.使用Texture2d类下的ReadPixels方法
创建一个空纹理,读取屏幕像素,保存问png文件
private void CaptureScreenshotNormal(Rect rect,string savePath,string fileName)
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
Destroy (screenShot);
string finalPath = savePath + "//" + fileN
System.IO.File.WriteAllBytes(finalPath, bytes);
Debug.Log("截屏了一张图片:"+finalPath);
3.ReadPixels+摄像机的RenderTexture方法针对某个相机截屏
让ReadPixels读的是某个相机渲染的像素
private void CaptureCamera(Camera camera, Rect rect,string fileName)
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture =
camera.Render();
RenderTexture.active =
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
GameObject.Destroy(rt);
camera.targetTexture = null;
RenderTexture.active = null;
byte[] bytes = screenShot.EncodeToPNG();
Destroy (screenShot);
System.IO.File.WriteAllBytes(fileName, bytes);
Debug.Log("截屏了一张图片:"+fileName);
三、实现上述需求
1.普通全屏截图
使用方法1,不解释;
使用方法2:Rect rect = new Rect (0,0,Screen.width,Screen.height);//全屏
2.普通局部截图
使用方法2:
Rect rect = new Rect (Screen.width/4,Screen.height/4,Screen.width/2,Screen.height/2);//半屏
3.针对某一相机的截图
使用方法3:
在脚本中定义一个public变量:public C
想截那个camera就拖哪个
4.去除UI元素截图
a、使用方法3
将管理UI元素的camera和管理场景的camera分开,拖管理场景的camera即可
b、基于NGUI的笨方法
思路:开始截图时先隐藏NGUI中的所有UI元素,然后截图,然后显示NGUI所有UI元素
实现:由于本人目前对显示隐藏不了解,所以用了更笨的方法,把UI root中的UI元素用container包起来,然后给container设计一个tween动画,只要能将container移出UI root的范围即可
这样在截图之前先执行该动画,在截图,最后再反向执行该动画。
代码如下:
public class SimpleCapNoUI : MonoBehaviour {
public TweenPosition hideUiT
public void OnNoUiCapClick(){
StartCoroutine(CaptureNoUI());
IEnumerator CaptureNoUI() {
hideUiTween.PlayForward ();
yield return new WaitForSeconds(0.01f);
Rect rect = new Rect (0,0,Screen.width,Screen.height);
string savePath = Application.dataP
string fileName = "SimpleCapNoUI.png";
CaptureScreenshotNormal (rect, savePath, fileName);
hideUiTween.PlayReverse ();
private void CaptureScreenshotNormal(Rect rect,string savePath,string fileName)
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
Destroy (screenShot);
string finalPath = savePath + "//" + fileN
System.IO.File.WriteAllBytes(finalPath, bytes);
Debug.Log("截屏了一张图片:"+finalPath);
实例图片:
当前game视窗
普通全屏截图
普通半屏截图
使用方法3实现的去除UI截图:注意立方体的位置,为什么?
使用方法3实现的只截取UI的截图
本人原创的笨方法实现的去UI截图
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3794次
排名:千里之外
原创:13篇
(6)(3)(4)(3)> 初心勿忘的博客详情
using&UnityE
using&System.C
public&class&cameraShake&:&MonoBehaviour
&&&private&Vector3&originP
&&&private&Quaternion&originR
&&&public&float&shake_
&&&public&float&shake_
&&&void&OnGUI&(){
&&&&&&if&(GUI.Button&(new&Rect&(20,40,80,20),&"Shake")){
&&&&&&&&&Shake&();
&&&void&Update&(){
&&&&&&if&(shake_intensity&&&0){
&&&&&&&&&transform.position&=&originPosition&+&Random.insideUnitSphere&*&shake_
&&&&&&&&&transform.rotation&=&new&Quaternion(
&&&&&&&&&originRotation.x&+&Random.Range&(-shake_intensity,shake_intensity)&*&.2f,
&&&&&&&&&originRotation.y&+&Random.Range&(-shake_intensity,shake_intensity)&*&.2f,
&&&&&&&&&originRotation.z&+&Random.Range&(-shake_intensity,shake_intensity)&*&.2f,
&&&&&&&&&originRotation.w&+&Random.Range&(-shake_intensity,shake_intensity)&*&.2f);
&&&&&&&&&shake_intensity&-=&shake_
&&&void&Shake(){
&&&&&&originPosition&=&transform.
&&&&&&originRotation&=&transform.
&&&&&&shake_intensity&=&.3f;
&&&shake_decay&=&0.002f;
人打赏支持
码字总数 5272
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区unity摄像机属性及妙用详解_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
unity摄像机属性及妙用详解
上传于||文档简介
&&详​细​了​解​U​n​i​t​y​相​机
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢}

我要回帖

更多关于 unity3d 正交相机 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信