- Home /
dynamically re-arranging gameobjects in grid layout group
Hi everyone,
I have a panel(dropAreaHolder) which has a scrollable panel(dropArea) as a child. In my game I instantiate game objects by drag and drops and put them inside my scrollable panel (dropArea) (just like the idea in Scratch, cloning code blocks and putting them in a coding area). My coding area can only show about 20 code blocks. When the number of code blocks is larger than 20, then my scrollbar will become visible and player can scroll and see everything. When the number of code blocks become 20 or less, scrollbar is hidden again and scroll rect component is disabled. Up to this point everything works just perfect. But the problem is that when i remove lets say the first code block, the code blocks do not re-arrange themselves so i can't see all 20 code blocks, the last one always remains at the bottom - out of the masked area.
It looks something like that:
Answer by xgg · Oct 15, 2017 at 09:39 PM
I have a problem somewhat like this and would like to know if you found a way to update the GridLayoutGroup.
My problem is when I SetActive(true) an object in the GridLayoutGroup(glg), glg does not update the positions, so the object does not align with the others like it is meant to...
I found a hotfix, which is the:
glg.gameObject.SetActive(false);
glg.gameObject.SetActive(true);
It somehow sets off a process which works for me.
Actually I kinda found a solution to my problem. Firs I made some arrangements in my script which is attached to my code blocks.
public void OnEndDrag(PointerEventData eventData) {
//checking if code block is removed from my coding area
if(parentToReturnTo == null)
{
if (Scene$$anonymous$$anager.GetActiveScene().buildIndex == 3)
{
//Sending index of the removed code block to activitycontroller scipt in the active scene
actController.reArrangeCodeBlocks(this.transform.GetSiblingIndex());
}
}
}
And in my activitycontroller script in the active scene i create a function to rearrange code blocks in the drop area:
public void reArrangeCodeBlocks(int removedCodeIndex) {
if (dropArea.transform.childCount == 19)
{
// changing index of the next code block
GameObject nextCodeBlock = dropArea.transform.GetChild(removedCodeIndex + 1).gameObject;
nextCodeBlock.transform.SetSiblingIndex(removedCodeIndex);
}
}
This solved my problem, I dont know if its the best solution to practice but changing the next code block's index automatically triggered the layout group to rearrange remaining ones for me.
Your answer
Follow this Question
Related Questions
Create placeholders in grid layout group? 1 Answer
Add Item to GridLayoutGroup using Instantiate 3 Answers
UI Layout Moves after you save or touch it in the Editor 0 Answers
Adding Imperfections? 1 Answer