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);
    }
相关推荐
孟无岐3 小时前
【Laya】Laya 类使用说明
typescript·游戏引擎·游戏程序·laya
在路上看风景5 小时前
1.2 Unity资源分类
unity·游戏引擎
one named slash5 小时前
BMFont在Unity中生成艺术字
unity·游戏引擎
梦想的旅途26 小时前
基于 UI 自动化(RPA)实现企业微信外部群操作的架构设计
ui·自动化·rpa
郝学胜-神的一滴7 小时前
图形学中的纹理映射问题:摩尔纹与毛刺的深度解析
c++·程序人生·unity·游戏引擎·图形渲染·unreal engine
谁怕平生太急7 小时前
MAI-UI的prompt
ui·prompt·gui agent·mai-ui
在路上看风景7 小时前
10. CPU-GPU协作渲染
unity
程序员agions8 小时前
Unity 游戏开发邪修秘籍:从入门到被策划追杀的艺术
unity·cocoa·lucene
JIes__8 小时前
Unity(一)——场景切换、退出游戏、鼠标隐藏锁定...
unity·游戏引擎