Search Unity

Overlapping canvas issue

Discussion in 'UGUI & TextMesh Pro' started by Ambrose998800, Dec 31, 2018.

  1. Ambrose998800

    Ambrose998800

    Joined:
    Jun 23, 2015
    Posts:
    74
    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.

    That's the code I use:

    Code (CSharp):
    1. void CheckPlace()
    2.     {
    3.         if (!Blocked)
    4.         {
    5.             RectTransform ThisCanvasRect = ItemCanvas.GetComponent<RectTransform>();
    6.  
    7.             foreach (Transform OtherCanvas in ItemCanvas.transform.parent)
    8.             {
    9.                 if (OtherCanvas.tag == "ItemCanvas" && OtherCanvas.gameObject != ItemCanvas)
    10.                 {
    11.                     RectTransform OtherCanvasRect = OtherCanvas.GetComponent<RectTransform>();
    12.  
    13.                     if (CanvasOverlapping(ThisCanvasRect, OtherCanvasRect))
    14.                     {
    15.                         Blocked = true;
    16.  
    17.                         DLF("position blocked by " + OtherCanvas.name);
    18.  
    19.                         break;
    20.                     }
    21.                 }
    22.             }
    23.         }
    24.     }
    25.  
    26.     bool CanvasOverlapping(RectTransform ThisCanvas, RectTransform OtherCanvas)
    27.     {
    28.         Rect TC = new Rect(ThisCanvas.localPosition.x, ThisCanvas.localPosition.y, ThisCanvas.rect.width, ThisCanvas.rect.height);
    29.         Rect OC = new Rect(OtherCanvas.localPosition.x, OtherCanvas.localPosition.y, OtherCanvas.rect.width, OtherCanvas.rect.height);
    30.  
    31.         //Rect TC = new Rect(ThisCanvas.rect.xMin, ThisCanvas.rect.yMin, ThisCanvas.rect.xMax, ThisCanvas.rect.yMax);
    32.         //Rect OC = new Rect(OtherCanvas.rect.xMin, OtherCanvas.rect.yMin, OtherCanvas.rect.xMax, OtherCanvas.rect.yMax);
    33.  
    34.         return TC.Overlaps(OC);
    35.     }
    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...
    How do I use the check correctly?