- Home /
How to detect the border of a canvas grid and fill the slots of the grid with different shapes of blocks?
Hello, I need some help with my grid system.
I created a grid system as an 2D-Array of GameObjects. Each of this GameObjects are Slots with a DropHandler Script. I also have a DragHandler Script to put dragged objects into the slot and let them snap into it. This works fine with a single object, which is the same size as the slot.
Now I want to drag different shapes into the grid (like Tetris blocks for example). These blocks are groups of the single object block. How do I define the border of the grid, so that no object can clip over it? This is the case for the object in the upper right corner of the grid shown in the picture.
I also want to detect which block of each group is in which slot of the grid, but when i take the group out of the grid it should stay as a group like before. In my current grid which is working with a single object block I added the block to the grid slot like shown in the picture. I don't know if that is also the right way for group blocks.
The detection of a full slot should not be that difficult, because I can access each slot of the grid from script through the array. But before that I need some help with this. I was testing several ways to do this the last days but with no success.
It should be working in the Canvas, because it is needed in the menu. Hope someone can help me with my problem.
Answer by Yukiarashi · Nov 27, 2020 at 12:34 PM
Solved my problem. Made a workaround with 2D Box Collider for every slot and dragged child of the box group with different tags. I also made a 2D Polygon Collider for the different shapes of the block groups. Then I used a 2D Edge Collider as border for the grid.
I don't know if this is good solution or not but it works fine for me now.
Answer by jimmiewalker653 · Nov 26, 2020 at 04:25 PM
Double check your anchor positions. Also, make sure the canvas is set to "Scale with Screen". Double checking and fixing both of those should fix your issue. If it doesn't then make sure the scale is set to 1,1,1 for all UI gameObjects. If they're not, then reset the scale to 1,1,1 and then move and anchor in the points as needed.
Thank you for your answer, but this is not exactly my problem. The problem is, that I attached the DragHandler Script to the group parent of the block group and not to every single block because I want to move the entire group at once. When I drag the group to a slot only this slot will be full and not the other ones around it. I can't attach the DragHandler Script to every child because then I separate the group.