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();
}