MaskableGraphic继承自Graphic,并且继承了IClippable, IMaskable, IMaterialModifier三个接口。它是RawImage、Image和Text的父类。

继承自Graphic的方法:

OnEnable:设置m_ShouldRecalculateStencil(是否需要重新计算模板)为true,调用UpdateClipParent(更新裁剪的父对象),调用SetMaterialDirty(设置材质为脏,Graphic的函数)。若有Mask组件,调用静态函数MaskUtilities.NotifyStencilStateChanged,重新计算Mask。

OnDisable:设置m_ShouldRecalculateStencil为true,依次调用 SetMaterialDirty和UpdateClipParent。在StencilMaterial中移除m_MaskMaterial,并把m_MaskMaterial设为null。若有Mask组件,调用静态函数MaskUtilities.NotifyStencilStateChanged。

OnTransformParentChanged(当父对象改变):设置 m_ShouldRecalculateStencil为true,依次调用UpdateClipParent、SetMaterialDirty。

OnCanvasHierarchyChanged(父对象的Canvas状态改变):设置 m_ShouldRecalculateStencil为true,依次调用UpdateClipParent、SetMaterialDirty。

看一下UpdateClipParent的代码:

     private void UpdateClipParent()
{
var newParent = (maskable && IsActive()) ? MaskUtilities.GetRectMaskForClippable(this) : null; // if the new parent is different OR is now inactive
if (m_ParentMask != null && (newParent != m_ParentMask || !newParent.IsActive()))
{
m_ParentMask.RemoveClippable(this);
UpdateCull(false);
} // don't re-add it if the newparent is inactive
if (newParent != null && newParent.IsActive())
newParent.AddClippable(this); m_ParentMask = newParent;
}

调用MaskUtilities.GetRectMaskForClippable找到父对象的RectMask2D组件(RectMask2D组件可以根据RectTransform裁剪子对象,子对象超出父RectTransform范围的部分会被裁剪掉)。

如果newParent不等于m_ParentMask或者newParent是未激活的,就调用RemoveClippable(在RemoveClippable中调用clippable.SetClipRect(new Rect(), false)关闭矩形裁剪,并且把自己从RectMask2D的m_ClipTargets中删除),然后更新剔除。

如果newParent激活,就调用AddClippable(把自己添加到RectMask2D的m_ClipTargets中)。

把newParent赋值给m_ParentMask。

继承自IClippable的方法:

RecalculateClipping(当父对象的IClippable状态改变时调用):调用UpdateClipParent。

Cull:如果validRect为false或者clipRect与rootCanvasRect矩形不重合,调用UpdateCull。UpdateCull中,如果canvasRenderer.cull不等于输入的cull,则canvasRenderer.cull=cull,回调m_OnCullStateChanged,再调用Graphic的OnCullingChanged函数。

SetClipRect:根据传入的validRect值,选择开启或者关闭canvasRenderer的矩形裁剪。

继承自IMaskable的方法:

RecalculateMasking:在StencilMaterial中移除m_MaskMaterial,把m_MaskMaterial设为null,m_ShouldRecalculateStencil设为true,调用SetMaterialDirty函数。

继承自IMaterialModifier的方法:

     public virtual Material GetModifiedMaterial(Material baseMaterial)
{
var toUse = baseMaterial; if (m_ShouldRecalculateStencil)
{
var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
m_StencilValue = maskable ? MaskUtilities.GetStencilDepth(transform, rootCanvas) : ;
m_ShouldRecalculateStencil = false;
} // if we have a enabled Mask component then it will
// generate the mask material. This is an optimisation
// it adds some coupling between components though :(
Mask maskComponent = GetComponent<Mask>();
if (m_StencilValue > && (maskComponent == null || !maskComponent.IsActive()))
{
var maskMat = StencilMaterial.Add(toUse, ( << m_StencilValue) - , StencilOp.Keep, CompareFunction.Equal, ColorWriteMask.All, ( << m_StencilValue) - , );
StencilMaterial.Remove(m_MaskMaterial);
m_MaskMaterial = maskMat;
toUse = m_MaskMaterial;
}
return toUse;
}

GetModifiedMaterial:如果m_ShouldRecalculateStencil为true,通过MaskUtilities.FindRootSortOverrideCanvas获取rootCanvas,根据maskable,给m_StencilValue赋值为模板深度或者0,m_ShouldRecalculateStencil设为false。

如果 m_StencilValue大于0且Mask组件不存在或者未激活,就把baseMaterial,stencilID,operation等参数添加到StencilMaterial中,并把m_MaskMaterial替换成新的材质。

UGUI之MaskableGraphic的更多相关文章

  1. Unity CCTween UGUI 动画插件

    在这简单的介绍一下 CCTween 动画插件的使用 因为GIF 制作软件不太好(网上随便下载的)所以导致效果不太好,有时间我重新制作一下 这是一下简单的效果 下面介绍怎么使用 首先 先下载 CCTwe ...

  2. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  3. ugui的优化

    参考文章 https://www.jianshu.com/p/061e67308e5f https://www.jianshu.com/p/8a9ccf34860e http://blog.jobbo ...

  4. UGUI 实现界面 渐隐渐现 FadeIn/Out 效果

    孙广东  2015.7.10 事实上熟悉NGUI的人,应该知道  实现渐隐渐现 FadeIn/Out 效果是非常方便的,由于父对象 的 改变会自己主动影响到子对象. 比方 UIWidget.UIPan ...

  5. UGUI性能优化

    http://www.cnblogs.com/suoluo/p/5417152.html http://blog.csdn.net/uwa4d/article/details/54344423 htt ...

  6. Unity3D中UGUI不使用DOTween制作渐隐渐现效果

    在做UI后期设计时,我们可能要对UI做一些特效,这篇文章我们来学习下如何在Unity3d中对实现渐隐渐现的效果, 首先我们看下Unity New UI即UGUI中渐隐渐现的做法. 观察我们会发现Uni ...

  7. 【UGUI源码分析】Unity遮罩之Mask详细解读

    遮罩,顾名思义是一种可以掩盖其它元素的控件.常用于修改其它元素的外观,或限制元素的形状.比如ScrollView或者圆头像效果都有用到遮罩功能.本系列文章希望通过阅读UGUI源码的方式,来探究遮罩的实 ...

  8. 【UGUI源码分析】Unity遮罩之RectMask2D详细解读

    遮罩,顾名思义是一种可以掩盖其它元素的控件.常用于修改其它元素的外观,或限制元素的形状.比如ScrollView或者圆头像效果都有用到遮罩功能.本系列文章希望通过阅读UGUI源码的方式,来探究遮罩的实 ...

  9. 如何快速优化手游性能问题?从UGUI优化说起

    WeTest 导读   本文作者从自身多年的Unity项目UI开发及优化的经验出发,从UGUI,CPU,GPU以及unity特有资源等几个维度,介绍了unity手游性能优化的一些方法.   在之前的文 ...

随机推荐

  1. 2016.1.22 扩充临时表空间解决ora-01652错误

    今天运行一个复杂查询时报错ora-01652 无法通过128 扩展temp段, 网上说是临时表空间大小不够,运行了脚本调整临时表空间,问题解决 alter database tempfile '/ap ...

  2. vue-router在新窗口打开页面

    1. <router-link>标签实现新窗口打开: <router-link target="_blank" :to="{path:'/app/dat ...

  3. 2018-2-13-win10-uwp-隐藏实时可视化

    title author date CreateTime categories win10 uwp 隐藏实时可视化 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 ...

  4. Apache ServiceMix介绍

    Apache ServiceMix介绍 Apache ServiceMix 是一个广泛使用的开源ESB,适合SOA项目的集成,它提供类似商业ESB产品一样的功能呢,它的核心是基于开放标准和规范. Se ...

  5. vmware虚拟机卸载干净在注册表的也需要删除

  6. DP刷题记录(持续更新)

    DP刷题记录 (本文例题目前大多数都选自算法竞赛进阶指南) TYVJ1071 求两个序列的最长公共上升子序列 设\(f_{i,j}\)表示a中的\(1-i\)与b中色\(1-j\)匹配时所能构成的以\ ...

  7. Scala中的函数表达式

    最近看Spark的东西,由于之前没有接触过lambda函数表达式,所以搜了点资料,特地纪录在此 Scala中的Lambda表达式 在函数式编程中,函数是基本的构造块.Scala融合了java中的面向对 ...

  8. 在eclipse动态网页项目中,编写web.xml时,servlet标签报错.

    cvc-complex-type.2.4.b: The content of element 'servlet' is not complete. One of '{"http:// jav ...

  9. c# 写个简单的爬虫。注:就一个方法,没有注释,自己猜~哈哈

    和我,在成都的街头走一走,哦~喔~哦~ public JsonResult GetHtml() { string url = "http://www.xxxxxxxxxxxxxxxxxx.c ...

  10. django框架(2)

    cookie和session 1.cookie不属于http协议范围, 由于http协议无法保持状态, 但实际情况, 我们却又需要"保持状态",因此cookie就是在这样一个场景下 ...