zoukankan      html  css  js  c++  java
  • Unity:截图

    三种截图方式:

      1. 引擎自带的截图方法

      2. 矩形截图

      3. 相机截图

    ————————————————————————————————————————————

    1. 引擎自带方法: ScreenCapture.CaptureScreenshot(“SavePath”);

    2. 矩形截图:

    1 private IEnumerator CaptureByRect(string fileName,Rect rect)
    2     {
    3         yield return new WaitForEndOfFrame();  //等当前帧渲染完了再截图
    4         Texture2D mTexture = new Texture2D((int)rect.width, (int)rect.height,TextureFormat.RGB24,false);
    5         mTexture.ReadPixels(rect, 0, 0);
    6         mTexture.Apply();
    7         byte[] bytes = mTexture.EncodeToPNG();
    8         System.IO.File.WriteAllBytes(fileName, bytes);
    9     }

    2.5 拓展:截取鼠标选取的矩形区域

     1 private void Update()
     2     {
     3 
     4         if (Input.GetMouseButtonDown(0))
     5         {
     6             start = Input.mousePosition;
     7         }
     8         if (Input.GetMouseButtonUp(0))
     9         {
    10             end = Input.mousePosition;
    11 
    12             Vector2 leftBottom = new Vector2(Mathf.Min(start.x, end.x), Mathf.Min(start.y, end.y));
    13             rect = new Rect(leftBottom.x, leftBottom.y, Mathf.Abs(end.x - start.x), Mathf.Abs(end.y - start.y));
    14             StartCoroutine(CaptureByRect(GetFilePath(), rect));
    15         }
    16     }

    3. 相机截图

     1 private IEnumerator CaptureByCamera(Camera mCamera,Rect mRect,string fileName)
     2     {
     3         yield return new WaitForEndOfFrame();
     4         RenderTexture mRender = new RenderTexture((int)mRect.width, (int)mRect.height, 0);
     5         mCamera.targetTexture = mRender;  //给相机新建一个RenderTexture
     6         mCamera.Render();  //渲染一次相机
     7         RenderTexture.active = mRender;   //激活RenderTexture
     8         Texture2D mTexture = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
     9         mTexture.ReadPixels(rect, 0, 0);
    10         mTexture.Apply();
    11         mCamera.targetTexture = null;
    12         Destroy(mRender);
    13         byte[] bytes = mTexture.EncodeToPNG();
    14         System.IO.File.WriteAllBytes(fileName, bytes);
    15     }
  • 相关阅读:
    ObjectArx的一次常用方法
    GDI+ 简介(1)
    VC++获取可执行文件当前目录
    SQL Server 常用的时间处理函数
    利于Wininet创建一个FTP客户端的步骤
    Win32 文件操作的几个API
    ObjectARX中三维多段线转二维多段线的方法
    fas文件格式解析
    [转载]swf文件格式解析(一)
    [转载]swf文件格式解析(二)
  • 原文地址:https://www.cnblogs.com/tomatokely/p/15646241.html
Copyright © 2011-2022 走看看