Unity 截屏

 //截屏
    public void CaptureScreen2(Camera c, Rect r)
    {
        //捕抓摄像机图像并转换成字符数组  
        RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);

        //targetTexture目标纹理
        c.targetTexture = rt;
        //Render手动渲染相机
        c.Render();

        //active目前活跃的渲染纹理
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
        //读取像素
        screenShot.ReadPixels(r, 0, 0);
        screenShot.Apply();

        //摄像机目标渲染纹理
        c.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);

        //将此纹理编码为PNG格式。
        byte[] bytes = screenShot.EncodeToPNG();

        string filename = @"d:\photo-whzq\Screenshot.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        PlayerPrefs.SetString(IMAGEFILE, filename); 


    }

  

猜你喜欢

转载自www.cnblogs.com/clhxxlcj/p/10972644.html