- Home /
Local canvas overlapping test
Hi there.
I almost completed my own inventory script. But the overlapping check for the item canvas is driving me crazy. I can't figure out how to check it properly.
The problem: with bigger item sizes, there is an invisible seam at the top of each canvas item that triggers the overlapping check. I checked the child elements of the canvas objects, they are all in place and the right size. The calculation must be the problem.
As you can see in the following picture (the backpack will block the belt):
an 1x1 item is not blocking at all
an 2x2 item works properly
an 3x3 item will block an additional row (but just above)
an 4x4 item will block two additional rows (again, just above)
and an 5x5 item will block three additional rows...
Here is the code I'm using for calculation:
void CheckPlace()
{
if (!Blocked)
{
RectTransform ThisCanvasRect = ItemCanvas.GetComponent<RectTransform>();
foreach (Transform OtherCanvas in ItemCanvas.transform.parent)
{
if (OtherCanvas.tag == "ItemCanvas" && OtherCanvas.gameObject != ItemCanvas)
{
RectTransform OtherCanvasRect = OtherCanvas.GetComponent<RectTransform>();
if (CanvasOverlapping(ThisCanvasRect, OtherCanvasRect))
{
Blocked = true;
DLF("position blocked by " + OtherCanvas.name);
break;
}
}
}
}
}
bool CanvasOverlapping(RectTransform ThisCanvas, RectTransform OtherCanvas)
{
Rect TC = new Rect(ThisCanvas.localPosition.x, ThisCanvas.localPosition.y, ThisCanvas.rect.width, ThisCanvas.rect.height);
Rect OC = new Rect(OtherCanvas.localPosition.x, OtherCanvas.localPosition.y, OtherCanvas.rect.width, OtherCanvas.rect.height);
//Rect TC = new Rect(ThisCanvas.rect.xMin, ThisCanvas.rect.yMin, ThisCanvas.rect.xMax, ThisCanvas.rect.yMax);
//Rect OC = new Rect(OtherCanvas.rect.xMin, OtherCanvas.rect.yMin, OtherCanvas.rect.xMax, OtherCanvas.rect.yMax);
return TC.Overlaps(OC);
}
Your answer
Follow this Question
Related Questions
Check if rotated UI are overlapping based on Rotation.Z 0 Answers
Rect.Contains and Rect.Overlap always return true 1 Answer
Overlap in Google Chrome and Safari 2 Answers
overlap between scenes!!! 1 Answer