本系列文章是学习siki学院UGUI整体解决方案-案例篇笔记
GitHub地址:https://github.com/BlueMonk1107/UGUISolution
本文实现的是在image点击不规则碰撞区域响应事件,效果如下 :

一.添加PolygonCollider2D组件,设置碰撞范围
可以选择Editor Collider直接拖动,也可以直接修改point

二.新建脚本,重写IsRaycastLocationValid
具体代码如下 :
public class CustomImage : Image
{
private PolygonCollider2D _polygon;
private PolygonCollider2D Polygon
{
get
{
if (_polygon == null)
_polygon = GetComponent<PolygonCollider2D>();
return _polygon;
}
}
public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
Vector3 point;
RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, screenPoint, eventCamera, out point);
return Polygon.OverlapPoint(point);
}
}
三.添加Button并添加响应事件

测试代码如下 :
public void MouseDown() {
Debug.Log("aaa");
}
这样点击脸部区域时会输出aaa,点击图片的其他区域不会输出
网友评论