I’m attempting to display an ordered list to the user of personnel inside a sector (labeled trisect).
This is a vertical layout group, and for the rows I’m using gameobject prefabs that when disabled are added to a pool. That pool holds on to disabled game objects until I need another one of that type, as to keep the scene uncluttered and save some memory.
The pooling is causing the sort to not work. If I disable the pool, it will sort people in barracks by their STR as intended. Without, it appears to have no sorting effect whatsoever.
I am attempting to sort via setsiblingindex(int i) or setLastSibling().
This works initially, but when the pooled items are reused, the order is off.
I’ve checked in debug to make sure that the soldier list sorts properly and even checked what values were sent in setsiblingindex(i).
What it revealed is that sibling index 0 has the most strength, but when I looked at the list, it showed that item nearly halfway down the list.
Pool code:
public GameObject AddPersonnelRow(Soldier sol)
{
GameObject personnelRow;
if (_inactivePersonnelRows.Count < 1)
{
personnelRow = GameObject.Instantiate(personnelRowPrefab);
personnelRow.transform.SetParent(personnelGrid);
}
else
{
personnelRow = _inactivePersonnelRows[0];
personnelRow.SetActive(true);
personnelRow.GetComponentInChildren<Toggle>().isOn = false;
_inactivePersonnelRows.RemoveAt(0);
}
return personnelRow;
}
public void RemovePersonnelRow(GameObject pRow)
{
_inactivePersonnelRows.Add(pRow);
pRow.SetActive(false);
pRow.transform.SetAsLastSibling();
}
Sort code:
private void Sort()
{
List<TrisectRow> soldiers;
switch (_trisect.trisect.station.Subtype)
{
case Subtypes.Barracks:
soldiers = _trisectPersonnel
.OrderByDescending(x => x.soldier.strength).ToList();
break;
case Subtypes.Research:
soldiers = _trisectPersonnel
.OrderByDescending(x => x.soldier.mind).ToList();
break;
default:
soldiers = _trisectPersonnel
.OrderByDescending(x => x.soldier.level)
.ThenByDescending(x => x.soldier.hp)
.ThenByDescending(x => x.soldier.strength).ToList();
break;
}
for (int i = 0; i < soldiers.Count(); i++)
{
soldiers*.mono.transform.SetAsLastSibling();*
}
}
TrisectRow class:
public class TrisectRow
{
public GameObject mono { get; set; }
public Soldier soldier { get; set; }
public TrisectRow(GameObject mono, Soldier soldier)
{
this.mono = mono;
this.soldier = soldier;
}
}
[85170-debugresults.png|85170]*
[85171-display.png|85171]*
*
*