一、说明
1、ClipperRegistry 类用来跟踪场景中所有 实现了 IClipper 接口的对象,然后在 CanvasUpdate 的循环期间剔除和裁剪可剪切元素。
2、已知目前实现了接口 IClipper 的只有 RectMask2D 。
3、已知目前实现了接口 IClippable 的只有 MaskableGraphic 。
实际上就是处理 RectMask2D 对其所有子 MaskableGraphic 的遮罩。
-----------------------------------------------------
二、全注释
1、ClipperRegistry
using System.Collections.Generic;
using UnityEngine.UI.Collections;
namespace UnityEngine.UI
{
// Registry class to keep track of all IClippers that exist in the scene
// This is used during the CanvasUpdate loop to cull clippable elements. The clipping is called after layout, but before Graphic update.
// 一个用于跟踪场景中所有 实现了 IClipper 接口的对象的 注册表类。
// 这是在 CanvasUpdate 循环期间用来剔除可剪切元素的。方法在 LayoutUpdate 与 GraphicUpdate 之间被调用。
public class ClipperRegistry
{
static ClipperRegistry s_Instance;
readonly IndexedSet<IClipper> m_Clippers = new IndexedSet<IClipper>();
protected ClipperRegistry()
{
// This is needed for AOT platforms. Without it the compile doesn't get the definition of the Dictionarys
// 这是 AOT 平台所需要的。没有它,编译器就得不到字典的的定义。
// 疑问???
#pragma warning disable 168
Dictionary<IClipper, int> emptyIClipperDic;
#pragma warning restore 168
}
// The singleton instance of the clipper registry.
// ClipperRegistry 单例
public static ClipperRegistry instance
{
get
{
if (s_Instance == null)
s_Instance = new ClipperRegistry();
return s_Instance;
}
}
// Perform the clipping on all registered IClipper
// 在所有注册的 IClipper 上执行裁剪
public void Cull()
{
for (var i = 0; i < m_Clippers.Count; ++i)
{
m_Clippers[i].PerformClipping();
}
}
// Register a unique IClipper element
// 注册一个特定 IClipper 元素
public static void Register(IClipper c)
{
if (c == null)
return;
instance.m_Clippers.AddUnique(c);
}
// UnRegister a IClipper element
// 将 IClipper 元素从注册中移除
public static void Unregister(IClipper c)
{
instance.m_Clippers.Remove(c);
}
}
}
2、IClipper 和 IClippable
namespace UnityEngine.UI
{
// Interface that can be used to recieve clipping callbacks as part of the canvas update loop.
// 可用于接受裁剪回调(裁剪是画布更新循环的一部分)的接口。
// 实现该接口的元素(目前只有RectMask2D),将对其“实现了 IClippable 接口”的子物体进行裁剪。
public interface IClipper
{
// Function to to cull / clip children elements.
// Called after layout and before Graphic update of the Canvas update loop.
// 在 CanvasUpdate 循环中, 方法在 LayoutUpdate 与 GraphicUpdate 之间被调用。
void PerformClipping();
}
// Interface for elements that can be clipped if they are under an IClipper
// 实现该接口的元素(目前只有MaskableGraphic),如果作为“实现了 IClipper 接口”的元素(目前只有RectMask2D)的子物体,则可被裁剪。
public interface IClippable
{
// 实现了 IClippable 接口的组件所在的 GameObject
GameObject gameObject { get; }
// Will be called when the state of a parent IClippable changed.
// “实现了 IClipper 接口”的元素(目前只有RectMask2D)状态改变时,调用其“实现了 IClippable 接口”的子物体的该方法。
// 状态改变包括:OnEnable、OnDisable、OnValidate(编辑器下)。
void RecalculateClipping();
// The RectTransform of the clippable.
// 实现了 IClippable 接口的组件关联的 RectTransform。
RectTransform rectTransform { get; }
// Clip and cull the IClippable given a specific clipping rect
// 裁剪和剔除 IClippable 给定的裁剪矩形
// 参数"clipRect":The Rectangle in which to clip against. 裁剪本物体的矩形 (来自RectMask2D)。
// 参数"validRect":Is the Rect valid. If not then the rect has 0 size. 矩形是否有效。若无效,则矩形的大小视为0(不裁剪)。
void Cull(Rect clipRect, bool validRect);
// Set the clip rect for the IClippable.
// 设置裁剪矩形。
// 参数"value":The Rectangle for the clipping. 裁剪本物体的矩形 (来自RectMask2D)。
// 参数"validRect":Is the rect valid. 矩形是否有效。
void SetClipRect(Rect value, bool validRect);
}
}