using System;
using UnityEngine;
using System.Collections; public class TestCamreaCapture1 : MonoBehaviour { private string imagesPath;
private int imageIndex = ; private RenderTexture rt;
private Texture2D screenShot; private System.Diagnostics.Stopwatch stopWatch; // Use this for initialization
void Start () {
imagesPath = Application.dataPath + "/../pic/";
stopWatch = new System.Diagnostics.Stopwatch (); rt = new RenderTexture (Screen.width,Screen.height, );
//将rt附给一个摄像机,这样这个摄像机实时渲染的结果就都在这个tex上了.
GetComponent<Camera>().targetTexture = rt;
screenShot = new Texture2D (Screen.width,Screen.height, TextureFormat.ARGB32, false);
} // Update is called once per frame
void Update () {
StartCoroutine (ScreenShoot());
} IEnumerator ScreenShoot(){
yield return new WaitForEndOfFrame ();
stopWatch.Start ();
ScreenSave ();
imageIndex++;
stopWatch.Stop();
Debug.Log (string.Format("generate one pic use {0} 'ms",stopWatch.ElapsedMilliseconds));
stopWatch.Reset ();
} private void ScreenSave(){
//记住当前渲染纹理.
var remeber = RenderTexture.active;
//将相机的渲染结果设置为当前渲染.
RenderTexture.active = rt;
//读取
screenShot.ReadPixels (new Rect (, , Screen.width, Screen.height), , );
byte[] bytes;
bytes = screenShot.EncodeToJPG ();
System.IO.File.WriteAllBytes (imagesPath + imageIndex +".jpg", bytes);
//将之前的渲染纹理变成激活.
RenderTexture.active = remeber; }
}
个人最喜欢这种方式,效率已经相当高了