0
\$\begingroup\$

I need to manage the index positions in a GridLayoutGroup and keep it consistent with the childCount of the group it belongs to.

The Grid has a fixed ConstraintCount(3) and can be increased or decreased by 3 (it adds a row). The index can also be modified (+1, -1) so that its value within the transform group changes. I initially wrote the code

        destTank = top.transform.childCount - 2;

which works well, it keeps my index destTank always to be on the top/middle position of the grid even when the grid changes, but it doesn't take in consideration the index variations. as result an index of 9 in a 3x3 grid will become 5 in a 3x2 grid but i need it to be 6. In other terms if the position on the grid is, let's say, on the top/left corner of the grid it should also be in the top/left corner in a smaller grid, not in the top/center position.

to solve this i wrote something like that

        destTank = top.transform.childCount - (top.transform.childCount - destTank);

but it's not working because the value of destTank is always changing and gives me weird results. I tried a lot of different combinations of values, even tried to divide the childCount by the contraintCount to obtain a ratio (1,2,3) corresponding to the rows number and use it somehow as a delta to obtain my index but nothing worked. Maybe I'm just messing up the syntax when writing the math operations. Anyone can help me solve the puzzle? Here's the full code:

    private void Positions()
{
    var rows = top.transform.childCount - 3;


    destTank = top.transform.childCount - (top.transform.childCount - destTank);
    destHealer = center.transform.childCount - 2; // (center.transform.childCount / GLG_center.constraintCount);
    destDpsMR = right.transform.childCount - 1;
    destDpsML = left.transform.childCount - 1;
    destDpsRR = bottomR.transform.childCount - 1;
    destDpsRL = bottomL.transform.childCount - 1;

    Debug.Log(top.transform.childCount);
}

private void PositionsPlus()
{
    if (currentGroupIndex == 0)
    {
        destTank = (destTank + 1) % top.transform.childCount;
    }
    if (currentGroupIndex == 1)
    {
        destHealer = (destHealer + 1) % center.transform.childCount;
    }
    if (currentGroupIndex == 2)
    {
        destDpsMR = (destDpsMR + 1) % right.transform.childCount;
    }
    if (currentGroupIndex == 3)
    {
        destDpsML = (destDpsML + 1) % left.transform.childCount;
    }
    if (currentGroupIndex == 4)
    {
        destDpsRR = (destDpsRR + 1) % bottomR.transform.childCount;
    }
    if (currentGroupIndex == 5)
    {
        destDpsRL = (destDpsRL + 1) % bottomL.transform.childCount;
    }
}

public void CellAdd(int value)
{
    for (int i = 0; i < value; i++)
    {
        if (currentGroup.transform.childCount < 9)
        {
            Instantiate(gridCell, currentGroup.transform.position, Quaternion.Euler(90, 0, 0), currentGroup.transform);
            FloatingText("+ " + value.ToString() + " CELLS");
        }
    }
    GroupsOffset();
    Positions();
}
public void CellRemove(int value)
{
    for (int i = value; i > 0; i--)
    {
        if (currentGroup.transform.childCount > 3)
        {
            DestroyImmediate(currentGroup.transform.GetChild(i).gameObject);
            FloatingText("- " + value.ToString() + " CELLS");
        }
    }
    GroupsOffset();
    Positions();
}

enter image description here enter image description here enter image description here enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ While the images help a bit, I'm still unclear what exactly you are asking. I get that the colors are falling/ be placed in a group of 3 but thats where I'm lost already. Are the blue ones supposed to fall down? Is that red tank thing placed at the wrong place? Maybe use a different image sequence with less dots in a three step, twice. Once how it is now and a second one how it is supposed to look like. (Thats 9 images in total, can be even simple paint dots) \$\endgroup\$
    – Zibelas
    Jun 30 at 20:43
  • \$\begingroup\$ sorry I added a new image to better explain what the problem is. let's focus on one group only (red), forget other colors. what i need is the index to be 6 in a 3x3 grid, 3 in a 3x2 and 0 in a 3x1. if i change the index manually (from 6 to 7 for example) then it should be 7, 4 and 1. if i set it to 8 it should become 5 and 2 and so on. \$\endgroup\$
    – bibrosko
    Jun 30 at 22:52

1 Answer 1

0
\$\begingroup\$

The calculation for your second image is easy.

circleNumber = ((totalElements - 1) / 3) * 3 + index;

We are doing a simple integer division. / 3 * 3 will give you the top left number of the latest row, from there you just add the index. If your index can go higher than 2 but should wrap around (meaning index 3 should point to 6 again), you add can modify it to

circleNumber = ((totalElements - 1) / 3) * 3 + (index % 3);
\$\endgroup\$
3
  • \$\begingroup\$ i don't understand what circleNumber stands for. i though it should be the same of index. i changed my script like this destTank = ((top.transform.childCount - 1) / 3) * 3 + destTank; but it's not working. do i have to create a new var called index? \$\endgroup\$
    – bibrosko
    Jul 1 at 13:27
  • \$\begingroup\$ The circled number from your screenshot is the result of the calculation, therefore circleNumber as a name. I assumed from your second screenshot that the index/ offset is known \$\endgroup\$
    – Zibelas
    Jul 1 at 14:36
  • \$\begingroup\$ the index = destTank. However it seems your second chunk of code perfectly works, and without even having to define an additional value (i wonder why). I think it's solved. \$\endgroup\$
    – bibrosko
    Jul 1 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .