最近在做一款拼图游戏,由于要设计到拖拽判断,UGUI中如何更方便的判断两个图片是否重叠呢?可参考如下方法:
public static bool IsRectTransformOverlap(RectTransform rect1, RectTransform rect2)
{
Vector3[] corners1 = new Vector3[4];
rect1.GetWorldCorners(corners1);
corners1[2].x = Mathf.Abs(corners1[2].x - corners1[0].x);
corners1[2].y = Mathf.Abs(corners1[2].y - corners1[0].y);
Rect b1 = new Rect(corners1[0].x, corners1[0].y, corners1[2].x, corners1[2].y);
rect2.GetWorldCorners(corners1);
corners1[2].x = Mathf.Abs(corners1[2].x - corners1[0].x);
corners1[2].y = Mathf.Abs(corners1[2].y - corners1[0].y);
Rect b2 = new Rect(corners1[0].x, corners1[0].y, corners1[2].x, corners1[2].y);
return b1.Overlaps(b2);
}