zoukankan      html  css  js  c++  java
  • Unity3d之截图

    1.Application.CaptureScreenshot("Screenshot.png", 0);  

    2.

    1. exture2D CaptureScreenshot2(Rect rect)   
    2. {  
    3.     // 先创建一个的空纹理,大小可根据实现需要来设置  
    4.     Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);  
    5.   
    6.     // 读取屏幕像素信息并存储为纹理数据,  
    7.     screenShot.ReadPixels(rect, 0, 0);  
    8.     screenShot.Apply();  
    9.   
    10.     // 然后将这些纹理数据,成一个png图片文件  
    11.     byte[] bytes = screenShot.EncodeToPNG();  
    12.     string filename = Application.dataPath + "/Screenshot.png";  
    13.     System.IO.File.WriteAllBytes(filename, bytes);  
    14.     Debug.Log(string.Format("截屏了一张图片: {0}", filename));  
    15.   
    16.     // 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。  
    17.     return screenShot;  
    18. }  
    19. 截全屏(如下图, 注:有ui):
      CaptureScreenshot2( new Rect( Screen.width*0f, Screen.height*0f, Screen.width*1f, Screen.height*1f));


      截中间4分之(如下图):
      CaptureScreenshot2( new Rect( Screen.width*0.25f, Screen.height*0.25f, Screen.width*0.5f, Screen.height*0.5f));

    3.

    1. Texture2D CaptureCamera(Camera camera, Rect rect)   
    2. {  
    3.     // 创建一个RenderTexture对象  
    4.     RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);  
    5.     // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机  
    6.     camera.targetTexture = rt;  
    7.     camera.Render();  
    8.         //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。  
    9.         //ps: camera2.targetTexture = rt;  
    10.         //ps: camera2.Render();  
    11.         //ps: -------------------------------------------------------------------  
    12.   
    13.     // 激活这个rt, 并从中中读取像素。  
    14.     RenderTexture.active = rt;  
    15.     Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);  
    16.     screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素  
    17.     screenShot.Apply();  
    18.   
    19.     // 重置相关参数,以使用camera继续在屏幕上显示  
    20.     camera.targetTexture = null;  
    21.         //ps: camera2.targetTexture = null;  
    22.     RenderTexture.active = null; // JC: added to avoid errors  
    23.     GameObject.Destroy(rt);  
    24.     // 最后将这些纹理数据,成一个png图片文件  
    25.     byte[] bytes = screenShot.EncodeToPNG();  
    26.     string filename = Application.dataPath + "/Screenshot.png";  
    27.     System.IO.File.WriteAllBytes(filename, bytes);  
    28.     Debug.Log(string.Format("截屏了一张照片: {0}", filename));  
    29.       
    30.     return screenShot;  
    31. }  
  • 相关阅读:
    将.net core api 部署成windows服务
    根据2个经纬度点,计算这2个经纬度点之间的距离(通过经度纬度得到距离)
    .NET 基础知识 单文件部署和可执行文件 剪裁独立部署和可执行文件
    通过 InnoSetup 美化安装界面
    拼凑一个ABP VNext管理后台拼凑一个ABP VNext管理后台
    互联网软件的安装包界面设计Inno setup
    weinre  远程实时调试手机上的Web页面 JAVASCRIPT远程调试
    asp.net core web应用以服务的方式安装运行
    用 vue2 和 webpack 快速建构 NW.js 项目
    谷歌插件抓包 similarweb抓包
  • 原文地址:https://www.cnblogs.com/huangshiyu13/p/5631394.html
Copyright © 2011-2022 走看看