屏幕录制

Record.cs

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;

public class Record : MonoBehaviour {

    public bool isForRecord = false;//是否记录
    public string filename = "Mov";   
    void Update()
    {
        if(isForRecord)
        {
            if(Input.GetKeyDown(KeyCode.R))
            {
                isForRecord = false;
                Time.timeScale = 1.0f;
            }
            string fn = filename + "(" + Time.frameCount + ")";
            StartCoroutine(ScreenShot(fn));
        }
    }

    IEnumerator ScreenShot(string ff)
    {
        int width = Screen.width;
        int height = Screen.height;
        yield return new WaitForEndOfFrame();
        Texture2D t = new Texture2D(width, height, TextureFormat.RGB24, false);//设置Texture2d
        t.ReadPixels(new Rect(0,0,width,height), 0, 0); //获取像素
        t.Apply();//设置进行应用
        byte[] bytes = t.EncodeToPNG();//进行转换
        Stream sm;//定义文件流
        if (Application.isEditor)//在编辑情况下
        {
            sm = new FileStream("E:/DaKuaYueScreenShot/" + ff + ".PNG" ,FileMode.Create );//文件流(路径,文件模式)
        }
        else
        {
            sm = new FileStream(Application.dataPath+"/Screenshots/" + ff + ".PNG",FileMode.Create);
        }
        BinaryWriter bw = new BinaryWriter(sm,Encoding.Unicode);//二进制转化
        bw.Write(bytes);//写入
        bw.Close();//关闭
        sm.Close();//文件流关闭
        Destroy(t);//销毁当前的Texture2D
    }
}

你可能感兴趣的:(屏幕录制)