unity 屏幕截图脚本

截取指定区域屏幕的截图

 

        public static Texture2D CaptureScreenshotByRectTransform(RectTransform rect)
        {
            Rect m_Rect = new Rect();
            Vector3[] fourCornersArray = new Vector3[4];
            rect.GetWorldCorners(fourCornersArray);
            m_Rect.width = fourCornersArray[2].x - fourCornersArray[0].x;
            m_Rect.height = fourCornersArray[2].y - fourCornersArray[0].y;
            m_Rect.x = fourCornersArray[0].x;
            m_Rect.y = fourCornersArray[0].y;
            return CaptureScreenshotByRect(m_Rect);
        }

 

        public static Texture2D CaptureScreenshotByRect(Rect rect)
        {
            // 先创建一个的空纹理,大小可根据实现需要来设置  
            Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
            // 读取屏幕像素信息并存储为纹理数据,  
            screenShot.ReadPixels(rect, 0, 0);
            screenShot.Apply();
            return screenShot;
        }

    

你可能感兴趣的:(unity)