当前位置: 首页 > news >正文

UI Toolkit generateVisualContent的使用

方法描述: 

Called when the VisualElement visual contents need to be (re)generated.


When this delegate is handled, you can generate custom geometry in the content region of the VisualElement. For an example, see the MeshGenerationContext documentation.

This delegate is called only when the VisualElement needs to regenerate its visual contents. It is not called every frame when the panel refreshes. The generated content is cached, and remains intact until any of the VisualElement's properties that affects visuals either changes, or VisualElement.MarkDirtyRepaint is called.

When you execute code in a handler to this delegate, do not make changes to any property of the VisualElement. A handler should treat the VisualElement as 'read-only'. Changing the VisualElement during this event might cause undesirable side effects. For example, the changes might lag, or be missed completely.

 上面将Unity中 UI Toolkit中的VisualElement相关文档抄录在这儿,总结出来就是在我们需要自定义一些UI图形时可以重写这个委托方法,生成我们想要的几何图形。有几点需要注意:

  • 这个方法不是每帧都调用,生成的内容会被缓存起来
  • 如果想重绘,可以调用VisualElement.MarkDirtyRepaint这个方法,重绘将在下一帧进行
  • 不要在重写的委托方法里去改变VisualElement的属性

那么,如何通过这个方法去生成我们想要的UI图形,这个委托本身是不会做这件事的,它是通过MeshGenerationContext 这个方法来帮我生成,这个类的对象可以通过generateVisualContent 回调用的传入参数中拿到。

在Unity的文档中关于这个类的使用有一个示例,抄录如下:

class TexturedElement : VisualElement{static readonly Vertex[] k_Vertices = new Vertex[4];static readonly ushort[] k_Indices = { 0, 1, 2, 2, 3, 0 };static TexturedElement(){k_Vertices[0].tint = Color.white;k_Vertices[1].tint = Color.white;k_Vertices[2].tint = Color.white;k_Vertices[3].tint = Color.white;}public TexturedElement(){generateVisualContent += OnGenerateVisualContent;m_Texture = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/tex.png");}Texture2D m_Texture;void OnGenerateVisualContent(MeshGenerationContext mgc){Rect r = contentRect;if (r.width < 0.01f || r.height < 0.01f)return; // Skip rendering when too small.float left = 0;float right = r.width;float top = 0;float bottom = r.height;k_Vertices[0].position = new Vector3(left, bottom, Vertex.nearZ);k_Vertices[1].position = new Vector3(left, top, Vertex.nearZ);k_Vertices[2].position = new Vector3(right, top, Vertex.nearZ);k_Vertices[3].position = new Vector3(right, bottom, Vertex.nearZ);MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length, m_Texture);// Since the texture may be stored in an atlas, the UV coordinates need to be// adjusted. Simply rescale them in the provided uvRegion.Rect uvRegion = mwd.uvRegion;k_Vertices[0].uv = new Vector2(0, 0) * uvRegion.size + uvRegion.min;k_Vertices[1].uv = new Vector2(0, 1) * uvRegion.size + uvRegion.min;k_Vertices[2].uv = new Vector2(1, 1) * uvRegion.size + uvRegion.min;k_Vertices[3].uv = new Vector2(1, 0) * uvRegion.size + uvRegion.min;mwd.SetAllVertices(k_Vertices);mwd.SetAllIndices(k_Indices);}}

上面的示例展示了如何将一张纹理绘制到UI上。这个过程和OpenGL绘制Texture比较像,可以看到Allocate这个方法接收顶点数据和索引数据,最后一个纹理对象是一个可选参数,如果不传,则绘制出来的是Vertex.tint赋值的颜色。

这里要注意 Vertex 类的 position 属性,它是以左上角为坐标原点,向右是x轴的正方向,向下是y轴的正方向。给定的顶点和索引的顺序要匹配,按顺时针的方向进行三角形的绘制。

假如有一个长宽都为200的VisualElement,我们给定三个点(0, 0), (200, 0), (0, 200)绘制一个三角形, 则它的索引顺序为0, 1, 2

http://www.lryc.cn/news/396517.html

相关文章:

  • 第十六章 ValidationPipe验证post请求参数
  • HippoRAG如何从大脑获取线索以改进LLM检索
  • 求函数最小值-torch版
  • 如何将HEVC格式的视频转换为无损、未压缩的MP4格式视频?
  • 自定义在线活动报名表单小程序源码系统 源代码+搭建部署教程 可二次定制开发
  • 数据分析入门指南:表结构数据(三)
  • 凌凯科技前五大客户依赖症加剧:研发费用率骤降,应收账款大增
  • 5 科大讯飞AI大赛:热力学定律的电池材料生产参数动态调控
  • 概论(二)随机变量
  • Apache AGE 安装部署
  • Python29 Tensorflow的基本知识和使用
  • Linux操作系统上用到的磁盘分区管理工具
  • Python数据结构的库之Fuk使用详解
  • 【STM32学习】cubemx配置,串口的使用,串口发送接收函数使用,以及串口重定义、使用printf发送
  • 复现MiDAS文章:文章数据和代码
  • 【Python专栏】Python的历史及背景介绍
  • web端已有项目集成含UI腾讯IM
  • IF不降反增!审稿速度,比我家网速还快!3本接受率高的医学期刊,赶紧码住!
  • 怎样把视频字幕提取出来?分享4个零门槛的字幕提取工具
  • PostgreSQL 里怎样解决多租户数据隔离的性能问题?
  • Oracle执行一条SQL的内部过程
  • SpringMVC的架构有什么优势?——控制器(一)
  • LabVIEW干涉仪测向系统
  • JavaScript 模拟光标全选选中一段文字
  • 【算法】代码随想录之数组(更新中)
  • Win-ARM联盟的端侧AI技术分析
  • MySQL常见的几种索引类型及对应的应用场景
  • 如何利用java依赖jave-all-deps实现视频格式转换
  • 三端保险丝-锂电池BMS二次保护器件
  • 用户增长 - 私域 - 社群运营自检清单SOP(社群运营30问)