原文链接:
http://blog.youkuaiyun.com/anyuanlzh/article/details/17008909
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CaptureScreen : MonoBehaviour {
void Start()
{
//Capture();
//StartCoroutine(Capture2(new Rect(0, 0, Screen.width, Screen.height)));
//Capture3(Camera.main, new Rect(0, 0, Screen.width, Screen.height));
Camera uiCamera = GameObject.Find("UICamera").GetComponent<Camera>();
Capture3(uiCamera, new Rect(0, 0, Screen.width, Screen.height));
}
public void Capture()
{
//保存在工程的根目录下
Application.CaptureScreenshot("Screenshot.png");
Debug.Log("Capture");
}
// 读取屏幕像素
public IEnumerator Capture2(Rect rect)
{
// 先创建一个的空纹理,大小可根据实现需要来设置
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
// 读取屏幕像素信息并存储为纹理数据
yield return new WaitForEndOfFrame();
screenShot.ReadPixels(rect, 0, 0, false);
screenShot.Apply();
// 然后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot2.png";
System.IO.File.WriteAllBytes(filename, bytes);
AssetDatabase.Refresh();
Debug.Log(string.Format("截屏了一张图片: {0}", filename));
}
// 读取RenderTexture像素
Texture2D Capture3(Camera camera, Rect rect)
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活这个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继续在屏幕上显示
camera.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 + "/Screenshot3.png";
System.IO.File.WriteAllBytes(filename, bytes);
AssetDatabase.Refresh();
Debug.Log(string.Format("截屏了一张照片: {0}", filename));
return screenShot;
}
}