# PolygonCollider2D

PolygonCollider2D 是 Unity 用于多边形检测的组件,可以进行不规则 UI、2d 对象的射线检测。

为 UI 对象添加如下脚本:

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 组件的 source 设置为上面的脚本即可。

# 参考文章

  1. Unity UGUI 学习系列 (二) ------ PolygonCollider2D 实现不规则碰撞范围_千喜的博客 - CSDN 博客
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Fasty 微信支付

微信支付

Fasty 支付宝

支付宝

Fasty 贝宝

贝宝