Unity截取UI

注释

unity 对某一矩形UI进行截取

cpp 复制代码
    /// <summary>
    /// 截取UI
    /// </summary>
    /// <param name="UIRect">UI(中点在最下面)</param>
    /// <param name="mFileName">路径</param>
    /// <returns></returns>
    public IEnumerator CaptureByUI(RectTransform UIRect, string mFileName)
    {
        //等待帧画面渲染结束
        yield return new WaitForEndOfFrame();

        Vector3[] corners = new Vector3[4];
        UIRect.GetWorldCorners(corners);
        float a = corners[2].x - corners[0].x;
        float b = corners[1].y - corners[0].y;
        Texture2D tex = new Texture2D((int)a, (int)b, TextureFormat.RGB24, false);
        //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
        tex.ReadPixels(new Rect(corners[0].x, corners[0].y, a, b), 0, 0);
        Debug.Log(new Rect(corners[0].x, corners[0].y, a, b));
        //执行读取操作
        tex.Apply();
        rawImageIcon.texture = tex;
        byte[] bytes = tex.EncodeToPNG();
        //保存
        File.WriteAllBytes(mFileName, bytes);
        //DownLoadImg(bytes);
    }
相关推荐
9 小时前
3D碰撞检测系统 基于SAT算法+Burst优化(Unity)
算法·3d·unity·c#·游戏引擎·sat
dzj202114 小时前
Unity是如何把3D场景显示到屏幕上的——Unity的渲染过程
3d·unity·游戏引擎·渲染·图形学
死也不注释1 天前
【鸡零狗碎记录】
unity·c#
白玉cfc1 天前
【iOS】网易云仿写
ui·ios·objective-c
★YUI★1 天前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
小马哥编程2 天前
如何解决 undetected_chromedriver 启动慢问题
chrome·selenium·ui
帧栈2 天前
开发避坑短篇(6):Vue+Element UI 深度选择器实现表单元素精准对齐的技术实践
vue.js·ui·elementui
吗喽对你问好2 天前
Android UI 控件详解实践
android·ui
★YUI★2 天前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
不绝1912 天前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎