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);

    }   

}

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

猜你喜欢

转载自blog.csdn.net/ONEMOOC/article/details/80040341