Unity中截图只渲染有像素的区域,并且压缩保存到本地

需求如标题,话不多说,上代码。

  void Start()
    {
        cam = gameObject.GetComponent();
        ScreenHeight = 1024;
        ScreenWidth = 1024;
    }

    public void StartGetCapture()
    {
        CaptureCamera(cam, rect);
    }
     Texture2D CaptureCamera(Camera camera)
    {
        // 创建一个RenderTexture对象  
        RenderTexture rt = new RenderTexture(ScreenWidth, ScreenHeight, 1);
        // 设置相关相机的targetTexture为rt, 并手动渲染相机  
        camera.targetTexture = rt;
        camera.Render();
        // 激活这个rt, 并从中中读取像素。  
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D(ScreenWidth, ScreenHeight, TextureFormat.ARGB32, false);//TextureFormat设置成ARGB32,只渲染有像素的区域。
        float vx = (v1.x > v2.x) ? v2.x : v1.x;      //取较小的x,y作为起始点
        float vy = (v1.y > v2.y) ? v2.y : v1.y;
        screenShot.ReadPixels(new Rect(0, 0, 1024, 1024), 0, 0, false);//读取像素
        screenShot.Apply();

        // 重置相关参数,让camera继续在屏幕上显示  
        camera.targetTexture = null;
        RenderTexture.active = null; // JC: added to avoid errors   
        GameObject.Destroy(rt);
        //压缩纹理的尺寸
        int targetWidth = (int)(ScreenWidth*0.5f);//压缩的比率
        int targetHeight =(int)(ScreenHeight*0.5f);//压缩的比率
        Texture2D result = new Texture2D(targetWidth, targetHeight, screenShot.format, false);

        float incX = (1.0f / (float)targetWidth);
        float incY = (1.0f / (float)targetHeight);

        for (int i = 0; i < result.height; ++i)
        {
            for (int j = 0; j < result.width; ++j)
            {
                Color newColor = screenShot.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
                result.SetPixel(j, i, newColor);
            }
        }

        result.Apply();
        // 最后将这些纹理数据,保存到一个png图片文件 
        byte[] bytes = result.EncodeToPNG();

        string filename = Application.dataPath + "/Screenshot.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        return screenShot;

    }

你可能感兴趣的:(Unity,unity)