我的notion笔记

Base class for all visual UI Component When creating visual UI components you should inherit from this class.

继承自ICanvasElement,ICanvasElement接口用在CanvasUpdateRegistry类中,在PerformUpdate(canvas的willRenderCanvases)时负责对注册的该类组件进行rebuild LayoutComplete GraphicUpdateComplete

/// <summary>
/// This is an element that can live on a Canvas.
/// </summary>
public interface ICanvasElement
{/// <summary>/// Rebuild the element for the given stage./// </summary>/// <param name="executing">The current CanvasUpdate stage being rebuild.</param>void Rebuild(CanvasUpdate executing);/// <summary>/// Get the transform associated with the ICanvasElement./// </summary>Transform transform { get; }/// <summary>/// Callback sent when this ICanvasElement has completed layout./// </summary>void LayoutComplete();/// <summary>/// Callback sent when this ICanvasElement has completed Graphic rebuild./// </summary>void GraphicUpdateComplete();/// <summary>/// Used if the native representation has been destroyed./// </summary>/// <returns>Return true if the element is considered destroyed.</returns>bool IsDestroyed();
}

protected Material m_Material;

默认材质球

private Color m_Color = Color.white;

Base color of the Graphic. The builtin UI Components use this as their vertex color. Use this to fetch or change the Color of visual UI elements, such as an Image.

public virtual Color color { get { return m_Color; } set { if (SetPropertyUtility.SetColor(ref m_Color, value)) SetVerticesDirty(); } }

private bool m_RaycastTarget = true

是否考虑射线检测

 /// <summary>
/// Should this graphic be considered a target for raycasting?
/// </summary>
public virtual bool raycastTarget { get { return m_RaycastTarget; } set { m_RaycastTarget = value; } }

SetAllDirty

设置layout、material、vertivces需要更新

SetLayoutDirty

layout那节有说,找到根节点的ILayout,加入m_LayoutRebuildQueue

        /// <summary>/// Mark the layout as dirty and needing rebuilt./// </summary>/// <remarks>/// Send a OnDirtyLayoutCallback notification if any elements are registered. See RegisterDirtyLayoutCallback/// </remarks>public virtual void SetLayoutDirty(){if (!IsActive())return;LayoutRebuilder.MarkLayoutForRebuild(rectTransform);if (m_OnDirtyLayoutCallback != null)m_OnDirtyLayoutCallback();}

SetMaterialDirty

加入m_GraphicRebuildQueue队列,同在CanvasUpdateRegistry的PerformUpdate中调用,在layout的rebuild和cull之后调用其rebuild。

private bool InternalRegisterCanvasElementForGraphicRebuild(ICanvasElement element)
{if (m_PerformingGraphicUpdate){Debug.LogError(string.Format("Trying to add {0} for graphic rebuild while we are already inside a graphic rebuild loop. This is not supported.", element));return false;}return m_GraphicRebuildQueue.AddUnique(element);
}

SetVerticesDirty

加入m_GraphicRebuildQueue队列,调用m_OnDirtyVertsCallback

OnEnable

  1. 调用RegisterGraphicForCanvas进行绑定,将canvas和graphic绑定。在射线检测时使用,获取canvas对应的graphic
  2. 初始化s_WhiteTexture为 Texture2D.whiteTexture;s_WhiteTexture为静态少属性
  3. 调用SetAllDirty(); 所以说SetActive的开销很大,会rebuild all
/// <summary>/// Mark the Graphic and the canvas as having been changed./// </summary>protected override void OnEnable(){base.OnEnable();CacheCanvas();GraphicRegistry.RegisterGraphicForCanvas(canvas, this);#if UNITY_EDITORGraphicRebuildTracker.TrackGraphic(this);
#endifif (s_WhiteTexture == null)s_WhiteTexture = Texture2D.whiteTexture;SetAllDirty();}

OnCanvasHierarchyChanged

层级改变时,重新RegisterGraphicForCanvas

OnCullingChanged

这可以用来执行之前由于Graphic被删除而被跳过的操作(Rebuild)。

 /// <summary>/// This method must be called when <c>CanvasRenderer.cull</c> is modified./// </summary>/// <remarks>/// This can be used to perform operations that were previously skipped because the <c>Graphic</c> was culled./// </remarks>public virtual void OnCullingChanged(){if (!canvasRenderer.cull && (m_VertsDirty || m_MaterialDirty)){/// When we were culled, we potentially skipped calls to <c>Rebuild</c>.CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);}}

OnTransformParentChanged

父节点改变时调用,和OnEnable逻辑差不多

Rebuild

在PreRender模式下,更新Mesh和材质球

/// <summary>
/// Rebuilds the graphic geometry and its material on the PreRender cycle.
/// </summary>
/// <param name="update">The current step of the rendering CanvasUpdate cycle.</param>
/// <remarks>
/// See CanvasUpdateRegistry for more details on the canvas update cycle.
/// </remarks>
public virtual void Rebuild(CanvasUpdate update)
{if (canvasRenderer == null || canvasRenderer.cull)return;switch (update){case CanvasUpdate.PreRender:if (m_VertsDirty){UpdateGeometry();m_VertsDirty = false;}if (m_MaterialDirty){UpdateMaterial();m_MaterialDirty = false;}break;}
}

UpdateGeometry

填充顶点数据,主要调用OnPopulateMesh接口。 同时获取grphic上其他的IMeshModifier组件,调用其ModifyMesh接口修改顶点数据 最后调用canvasRenderer.SetMesh(workerMesh);设置填充的顶点数据

private void DoMeshGeneration()
{if (rectTransform != null && rectTransform.rect.width >= 0 && rectTransform.rect.height >= 0)OnPopulateMesh(s_VertexHelper);elses_VertexHelper.Clear(); // clear the vertex helper so invalid graphics dont draw.var components = ListPool<Component>.Get();GetComponents(typeof(IMeshModifier), components);for (var i = 0; i < components.Count; i++)((IMeshModifier)components[i]).ModifyMesh(s_VertexHelper);ListPool<Component>.Release(components);s_VertexHelper.FillMesh(workerMesh);canvasRenderer.SetMesh(workerMesh);
}[Obsolete("Use OnPopulateMesh(VertexHelper vh) instead.", false)]
/// <summary>
/// Callback function when a UI element needs to generate vertices. Fills the vertex buffer data.
/// </summary>
/// <param name="m">Mesh to populate with UI data.</param>
/// <remarks>
/// Used by Text, UI.Image, and RawImage for example to generate vertices specific to their use case.
/// </remarks>
protected virtual void OnPopulateMesh(Mesh m)
{OnPopulateMesh(s_VertexHelper);s_VertexHelper.FillMesh(m);
}/// <summary>
/// Callback function when a UI element needs to generate vertices. Fills the vertex buffer data.
/// </summary>
/// <param name="vh">VertexHelper utility.</param>
/// <remarks>
/// Used by Text, UI.Image, and RawImage for example to generate vertices specific to their use case.
/// </remarks>
protected virtual void OnPopulateMesh(VertexHelper vh)
{var r = GetPixelAdjustedRect();var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height);Color32 color32 = color;vh.Clear();vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(0f, 0f));vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(0f, 1f));vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(1f, 1f));vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(1f, 0f));vh.AddTriangle(0, 1, 2);vh.AddTriangle(2, 3, 0);
}

UpdateMaterial

更新canvasRenderer的材质球和纹理

/// <summary>
/// Call to update the Material of the graphic onto the CanvasRenderer.
/// </summary>
protected virtual void UpdateMaterial()
{if (!IsActive())return;canvasRenderer.materialCount = 1;canvasRenderer.SetMaterial(materialForRendering, 0);canvasRenderer.SetTexture(mainTexture);
}

Raycast

由GraphicRaycaster 调用

  1. 由当前节点向父节点进行遍历
  2. 遍历节点的所有component
  3. 如果有canvas组件且设置了overrideSorting则不再遍历
  4. 获取ICanvasRaycastFilter组件,调用filter.IsRaycastLocationValid(sp, eventCamera);
  5. 如果返回值为false,返回结果false,否则继续遍历
/// <summary>
/// When a GraphicRaycaster is raycasting into the scene it does two things. First it filters the elements using their RectTransform rect. Then it uses this Raycast function to determine the elements hit by the raycast.
/// </summary>
/// <param name="sp">Screen point being tested</param>
/// <param name="eventCamera">Camera that is being used for the testing.</param>
/// <returns>True if the provided point is a valid location for GraphicRaycaster raycasts.</returns>
public virtual bool Raycast(Vector2 sp, Camera eventCamera)
{if (!isActiveAndEnabled)return false;var t = transform;var components = ListPool<Component>.Get();bool ignoreParentGroups = false;bool continueTraversal = true;while (t != null){t.GetComponents(components);for (var i = 0; i < components.Count; i++){var canvas = components[i] as Canvas;if (canvas != null && canvas.overrideSorting)continueTraversal = false;var filter = components[i] as ICanvasRaycastFilter;if (filter == null)continue;var raycastValid = true;var group = components[i] as CanvasGroup;if (group != null){if (ignoreParentGroups == false && group.ignoreParentGroups){ignoreParentGroups = true;raycastValid = filter.IsRaycastLocationValid(sp, eventCamera);}else if (!ignoreParentGroups)raycastValid = filter.IsRaycastLocationValid(sp, eventCamera);}else{raycastValid = filter.IsRaycastLocationValid(sp, eventCamera);}if (!raycastValid){ListPool<Component>.Release(components);return false;}}t = continueTraversal ? t.parent : null;}ListPool<Component>.Release(components);return true;
}

IsRaycastLocationValid

Image、Mask、RectMask2D重写了该方法

RectMask2D、Mask

直接调用RectTransformUtility.RectangleContainsScreenPoint(rectTransform, sp, eventCamera);

Image

增加了alpha点击测试,如果alphaHitTestMinimumThreshold在(0,1]之间,会进行判断点击的像素是否大于该值。 x,y是一系列坐标转换得来的activeSprite.texture.GetPixelBilinear(x, y).a >= alphaHitTestMinimumThreshold;

PixelAdjustPoint和GetPixelAdjustedRect

处理canvas.pixelPerfect

强制画布中的元素按像素对齐。仅在 renderMode 为屏幕空间时适用。

启用 pixelPerfect 可使元素看起来更清晰,避免出现模糊。但是,如果许多元素被缩放或旋转过,或者使用了微妙的动画位置或缩放,则禁用 pixelPerfect 可能比较好,这样可使移动更为平滑。

CrossFadeColor

Tweens the CanvasRenderer color associated with this Graphic.变色功能 利用协程,每帧更新已存活时间,除以总时长,得到已存活时间百分比,使用lerp获得当前值

UGUI——Graphic相关推荐

  1. Unity中的UGUI源码解析之图形对象(Graphic)(2)-ICanvasElement

    Unity中的UGUI源码解析之图形对象(Graphic)(2)-ICanvasElement 在上一篇文章中, 我们对整个Graphic部分做了概述, 这篇文章我们介绍ICanvasElement和 ...

  2. Unity3D学习笔记(十九):UGUI、Image、Text、Button

    UGUI:Unity官方最新,与NGUI同源 UI:User Interface(用户的操作界面),图片+文字 UGUI的组件: 1.创建UGUI组件时,会默认创建Canvas(画布)和EventSy ...

  3. Unity4.6新UI系统初探(uGUI)

    文章目录[点击展开](?)[+] 一引言 二Rect Transform 三排序 四控件 1 Image 2 Button 五事件 1 Event Trigger 2 Graphic Raycaste ...

  4. Unity3D - UGUI的手动搭建

    了解UGUI组件的搭建方式,有助于搭建我们自己的UI界面. Text 文本 text 是UGUI中的基本控件,在Hierarchyi面板创建一个空物体 - 给这个空物体添加一个Text组件即可实现与t ...

  5. 【转】(五)unity4.6Ugui中文教程文档-------概要-UGUI Interaction Components

    原创至上,移步请戳:(五)unity4.6Ugui中文教程文档-------概要-UGUI Interaction Components 4.Interaction Components 本节涵盖了处 ...

  6. Unity中UGUI之Canvas属性解读版本二

    Canvas Render Modes(渲染模式) 1.在screen空间中渲染 2.在world空间中渲染 Screen Space-Overlay 在这个渲染模式中,UI元素将在场景的上面.如果场 ...

  7. 使用UGUI绘制自定义几何图形

    本文展示了如何使用UGUI绘制矩形,同理可绘制其他几何图形. UGUI的渲染体系,简单来说所有的控件和可显示的元素都是Graphic.Graphic持有一个CanvasRenderer,通过SetVe ...

  8. ugui unity 取消选择_UGUI中几种不规则按钮的实现方式

    前言 UGUI中的按钮默认是矩形的,若要实现非矩形按钮该怎么做呢?比如这样的按钮: 本文将介绍两种实现方式供大家选择. 使用alphaHitTestMinimumThreshold Image类的al ...

  9. mesh渲染到ui_在Unity中使用UGUI修改Mesh绘制几何图形

    Used by Text, Image, and RawImage for example to generate vertices specific to their use case. 说的是当该 ...

最新文章

  1. 代码重构之三种取代类型码(类、子类、状态对象或策略对象)的方式辨析
  2. PHP 中日期时间函数 date() 用法总结
  3. docker停止容器,删除镜像常用指令
  4. 在 Windows 7 中禁用IPv6协议/IPv6隧道
  5. Java基础---认识抽象类
  6. spark学习-53-Spark下Java版HBase下的根据权重获取最真实数据
  7. C++模板学习:函数模板、结构体模板、类模板
  8. java报505_Java调用URL错误,报505
  9. spring 核心技术
  10. 计划性分析的要点,走向更好的关键
  11. Linux学习整理-网络防火墙iptables-实践篇2
  12. 【PART 1】OAK-D+TurtleBot3机器人项目全解析:SLAM、ROS、深度图、点云。
  13. Mac Pro硬盘清理,为啥我的系统占用如此之多的磁盘空间
  14. kof'97 出招表
  15. nginx指定目录安装
  16. Flink入门篇-编译源码(上)
  17. bigemap如何设置等高线坐标系并输出
  18. labview2020图文教程LabVIEW2020
  19. 笔记:项目中隐性知识共享的实施步骤
  20. 相继上一篇,thingsboard的二次开发环境配置和简单的logo替换

热门文章

  1. Java-计算器模拟程序
  2. 2005年大二下学期总结
  3. u-boot启动分析02(board_init_f,board_init_r)
  4. 【请先看这个,别慌看“1月12日”那个】根据之前文章整理的在2021年继续使用Flash Player的方法【22.5.10重新整理】
  5. 监控回放出现不支持的码流类型字样(不断总结)
  6. ClipDrawable资源的使用实例
  7. 帕累托法则(Pareto‘s principle)如何运用_Mr.D
  8. Win10不能直接拖动文件打开的解决办法
  9. hp 8440p笔记本电脑改装XP操作系统小记
  10. 4000 essential english words推荐学习