Unity中实现截图操作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class CoroutineTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        StartCoroutine(ScreenShotPNG());

    }
    IEnumerator ScreenShotPNG()
    {
        yield return new WaitForEndOfFrame();
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, width, height),0,0);
        tex.Apply();
        byte[] bytex = tex.EncodeToPNG();
        Destroy(tex);
        File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytex);

    }   

}

这里使用了一个协程函数,目的是使整个画面渲染完成,进入下一帧渲染后,在来完成截图操作。

你可能感兴趣的:(Unity3D,Unity,协程,Texture)