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);
    }
相关推荐
wonder135795 小时前
UGUI重建流程和优化
unity·游戏开发·ugui
reddingtons6 小时前
PS 参考图像:线稿上色太慢?AI 3秒“喂”出精细厚涂
前端·人工智能·游戏·ui·aigc·游戏策划·游戏美术
喜欢踢足球的老罗6 小时前
Swagger UI 自定义请求头:从用户配置到请求注入的完整流程解析
ui
Doc.S8 小时前
多无人机任务自定义(基于ZJU-FAST-Lab / EGO-Planner-v2)
游戏引擎·无人机·cocos2d
那个村的李富贵9 小时前
Unity打包Webgl后 本地运行测试
unity·webgl
nnsix10 小时前
Unity OpenXR开发HTC Vive Cosmos
unity·游戏引擎
nnsix10 小时前
Unity OpenXR,扳机键交互UI时,必须按下扳机才触发
unity·游戏引擎
nnsix11 小时前
Unity XR 编辑器VR设备模拟功能
unity·编辑器·xr
老朱佩琪!11 小时前
Unity访问者模式
unity·游戏引擎·访问者模式
Just_Paranoid11 小时前
【Android UI】Android Drawable XML 标签解析
android·ui·vector·drawable·shape·selector