Unity 重写image的点击区域

目前看网上所说的更改image的重写区域主要是通过更改对比的阈值,这种情况只适合,含有透明区域的image
image.alphaHitTestMinimumThreshold = alpahThreshold;

最近公司需求只对标签上方的一片区域点击,但是ui切图的时候没有分成两部分,时间又比较紧张,只有自己去重写image的可点击区域了,大概思路是把屏幕坐标转化为image的ui坐标,在通过判断点的y值来取上方的可点击区域

public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{

    if (sprite.name == "btn")
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent(), screenPoint, eventCamera, out screenPoint);
        return base.IsRaycastLocationValid(screenPoint, eventCamera) && (screenPoint.y >0);
        Debug.Log(screenPoint);

    }
    else
    {
        return base.IsRaycastLocationValid(screenPoint, eventCamera);
    }
}

你可能感兴趣的:(Unity)