Position object over center of cell of Grid Layout Group

Hello,

I can’t find any method witch allow me to center an object over center of cell of Grid Layout Group. There is Grid object with Grid Layout Group and with Cell objects within it. And draggable object. When I drag and drop object over cell I want to center with it.
6721096--773155--Screen Shot 2021-01-13 at 20.12.00.png 6721096--773158--Screen Shot 2021-01-13 at 20.16.49.png
I could make draggable object parent of cell, but the issue is object can be bigger than one cell.

Each cell implements IDropHandler

public void OnDrop(PointerEventData eventData)
     {
         GameObject itemObject = eventData.pointerDrag;
         if (itemObject != null)
         {
             //itemObject.transform.SetParent(transform);
             //itemObject.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
         }
     }

There is a field on the GridLayoutGroup to align child objects… it’s called “Child Alignment…” have you set that to Middle Center?

If that does not work, perhaps your Image thingy isn’t centered properly within itself.

Grid with its children is positioned properly. The issue, with another object which is outside of grid. I want to cover over cell with draggable object when I drag and drop it onto cell. I need some script to position the draggable object over cell at center. The cell size is 45 to 45. Item object is 32 x 32. So when I hover over cell and drop it I need to recalculate the position of Item so it would be in the middle of under laying cell.

1 Like

I think I would probably just clone the GridLayoutGroup at runtime and blast it full of empty GameObjects, then just reach for the one you want and use its position to center the visible object in the real grid.