指引遮罩层的作用,主要是屏蔽事件,指定区域穿透事件。在视觉上,遮挡是非遮挡区域有不同的颜色表示。我们分别从事件功能,和视觉功能上来分别介绍。事件功能需要脚本,视觉功能需要shader辅助。本文主要介绍事件功能的实现,shader的视觉辅助下篇介绍。
事件功能上,我们建立四个遮挡层,每个层都会阻挡事件。让后利用4块遮挡层,去围成一个矩形区域形成可点击区域。我们生成一个prefab,里面包含4个屏蔽层。动态的去计算4个屏蔽层的位置。
public RectTransform top;
public RectTransform bottom;
public RectTransform left;
public RectTransform right;
private void SetRectCenterInner(Vector2 center, Vector2 size)
{
RectTransform root = uiRoot.GetComponent<RectTransform>();
Vector2 half = root.sizeDelta * root.localScale.x / 2;
Vector2 halfSize = size * root.localScale.x / 2;
top.sizeDelta = root.sizeDelta;
bottom.sizeDelta = root.sizeDelta;
left.sizeDelta = root.sizeDelta;
right.sizeDelta = root.sizeDelta;
Vector2 tl = new Vector2(center.x - halfSize.x, center.y + halfSize.y);