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);
    }
相关推荐
天人合一peng6 小时前
Unity中做表头时像work中整个调整宽窄
unity
会一点设计6 小时前
6个优质春节海报模板网站推荐!轻松设计马年祝福海报
ui·ux
hudawei99617 小时前
TweenAnimationBuilder和AnimatedBuilder两种动画的比较
flutter·ui·动画·tweenanimation·animatedbuilder
依米阳光0818 小时前
Playwright MCP AI实现自动化UI测试
ui·自动化·playwright·mcp
小李也疯狂18 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的18 小时前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y18 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤18 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里19 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ19 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎