WhiteEngine-专注图形渲染与游戏引擎技术的网站

新浪微薄腾讯微薄

最新碎语:暂无碎语

您的位置:WhiteEngine-专注图形渲染与游戏引擎技术的网站 >引擎相关> Unity截图截屏

Unity截图截屏


//全屏截图

public void CaptureScreen()

    {
        Application.CaptureScreenshot("abc.png", 0);

    }

//自定义截图大小截图

public Texture2D CaptureScreen(Rect rect)
    {
        Texture2D screen = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screen.ReadPixels(rect, 0, 0);
        screen.Apply();
        byte[] bytes = screen.EncodeToPNG();
        string file = Application.dataPath + "/Screen.png";
        System.IO.File.WriteAllBytes(file, bytes);
        return screen;

    }

//不同的相机进行截图

 public Texture2D CapScreen(Camera c, Rect r)
    {
        RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);
        c.targetTexture = rt;
        c.Render();
        RenderTexture.active = rt;
        Texture2D screen = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
        screen.ReadPixels(r, 0, 0);
        screen.Apply();
        c.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        byte[] bytes = screen.EncodeToPNG();
        string filename = Application.dataPath + "/Screen.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        return screen;
    }




---

转载请注明本文标题和链接:《Unity截图截屏

分享到:

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交