zoukankan      html  css  js  c++  java
  • Unity3d之如何截屏

    Unity3d中有时有截屏的需求,那如何截屏呢,代码如下:

        /// <summary>
        /// 截屏
        /// </summary>
        /// <param name="camera">截屏的摄像机</param>
        /// <param name="rect">图片大小</param>
        /// <returns>屏幕快照</returns>
        Texture2D CaptureCamera(Camera camera, Rect rect)
        {
            RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
            RenderTexture originRT = camera.targetTexture;      // 临时把camera中的targetTexture替换掉
            camera.targetTexture = rt;
            camera.RenderDontRestore();                         // 手动渲染
            camera.targetTexture = originRT;
    
            RenderTexture.active = rt;
            Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);
            screenShot.ReadPixels(rect, 0, 0);                  // 读取的是 RenderTexture.active 中的像素
            screenShot.Apply();
            GameObject.Destroy(rt);
            RenderTexture.active = null;
    
            return screenShot;
        }

     如何保存 Texture2D 图片:

        static void SaveImage(Texture2D image, string path)
        {
            byte[] buffer = image.EncodeToPNG();
            File.WriteAllBytes(path, buffer);
        }

    转载请注明出处:http://www.cnblogs.com/jietian331/p/6899725.html

  • 相关阅读:
    抽象类abstract
    final关键字特点
    继承ExtendsFour
    继承(继承中构造方法的关系)
    继承ExtendsTwo-super&this
    继承ExtendsOne
    静态
    构造方法与setXxx方法
    15.8
    15.7
  • 原文地址:https://www.cnblogs.com/jietian331/p/6899725.html
Copyright © 2011-2022 走看看