{"id":4726,"date":"2023-03-20T17:11:02","date_gmt":"2023-03-20T09:11:02","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4726"},"modified":"2023-03-20T17:19:39","modified_gmt":"2023-03-20T09:19:39","slug":"unity%e6%88%aa%e5%b1%8f%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4726","title":{"rendered":"Unity\u622a\u5c4f\u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u3010\u8f7d\u5c4f\u5230\u6587\u4ef6\u3011\u6162\nScreenCapture.CaptureScreenshot(\"xxx.png\")\n\u56e0\u4e3aCPU\u8981\u5411GPU\u7b49\u5f85\u6570\u636e\uff0c\u975e\u5e38\u5361\uff0c\u63a8\u8350\n\n\n\u3010\u5168\u91cf\u8f7d\u5c4f\u5230\u7eb9\u7406\u3011\u5feb\n\/\/ \u5148\u7b49\u4e00\u5e27\nyield return new WaitForEndOfFrame();\nvar screentex= RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, \"rt\", false, false);\n\/\/ \u5168\u91cf\u6e32\u4e00\u5e27\nScreenCapture.CaptureScreenshotIntoRenderTexture(rt);\n\/\/ \u83b7\u5f97\u5e73\u53f0\u5dee\u5f02\nvar rect = SystemInfo.graphicsUVStartsAtTop ? new Vector4(1.0f, -1.0f, 0.0f, 1.0f) : new Vector4(1.0f, 1.0f, 0.0f, 0.0f);\nvar displayTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, \"displayTex\", false, false, FilterMode.Bilinear);\n\/\/ \u753b\u5230\u6307\u5b9a\u7eb9\u7406\u4e0a\nGraphics.Blit(screentex, displayTex, new Vector2(rect.x, rect.y), new Vector2(rect.z, rect.w));\nRenderTexture.ReleaseTemporary(screentex);\n\n\n\u3010\u4e0d\u5e26UI\u622a\u56fe\u5230RT\u3011\u5feb\nvar mainCamera = GetComponent&lt;Camera>();\nif (mainCamera != null)\n{\n  var screentex= RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, \"rt\", false, false);\n  var lastTex = mainCamera.targetTexture;\n  mainCamera.targetTexture = screentex;\n  mainCamera.Render();\n  mainCamera.targetTexture = lastTex;\n}\n\n\n\u3010\u76f4\u63a5\u8bfb\u3011\u6162\nTexture2D CaptureScreenshot2(Rect rect)\n{\n    Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);\n    screenShot.ReadPixels(rect, 0, 0);\n    screenShot.Apply();\n    byte[] bytes = screenShot.EncodeToPNG();\/\/\u7136\u540e\u5c06\u8fd9\u4e9b\u7eb9\u7406\u6570\u636e\uff0c\u6210\u4e00\u4e2apng\u56fe\u7247\u6587\u4ef6\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4726"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4726"}],"version-history":[{"count":7,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4726\/revisions"}],"predecessor-version":[{"id":4733,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4726\/revisions\/4733"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}