标签:
在游戏截屏时,我们需要只截取场景内容,而不包括NGUI控件,实现脚本如下(只截取gameCamera和bgCamera中的内容):
using UnityEngine; using System.Collections; public class CaptureTest : MonoBehaviour { public Camera gameCamera; public Camera bgCamera; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnClick() { Rect rect=new Rect(0,0,Screen.width,Screen.height); CaptureCamera(gameCamera, bgCamera, rect); } Texture2D CaptureCamera(Camera gameCamera,Camera bgCamera, Rect rect) { // 创建一个RenderTexture对象 RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0); // 设置两个camera的targetTexture gameCamera.targetTexture = rt; bgCamera.targetTexture = rt; //注意渲染顺序 bgCamera.Render(); gameCamera.Render(); // 激活这个rt, 并从中中读取像素。 RenderTexture.active = rt; Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素 screenShot.Apply(); // 重置相关参数,以使用camera继续在屏幕上显示 gameCamera.targetTexture = null; bgCamera.targetTexture = null; //ps: camera2.targetTexture = null; RenderTexture.active = null; // JC: added to avoid errors GameObject.Destroy(rt); // 最后将这些纹理数据,成一个png图片文件 byte[] bytes = screenShot.EncodeToPNG(); string filename = Application.dataPath + "/Screenshot.png"; System.IO.File.WriteAllBytes(filename, bytes); Debug.Log(string.Format("截屏了一张照片: {0}", filename)); return screenShot; } }
标签:
原文地址:http://www.cnblogs.com/cowill/p/4264255.html