Remove call to Cast, avoid alloc of Transform.Enumerator

Children are now retrieved using a combination of childCount and GetChild method
This commit is contained in:
Oliver Booth 2022-05-09 19:47:02 +01:00
parent 4d19e2f64c
commit d17e1670de
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
1 changed files with 8 additions and 10 deletions

View File

@ -184,27 +184,25 @@ public static class GameObjectExtensions
throw new ArgumentNullException(nameof(gameObject));
}
gameObject.layer = layer;
var children = new Stack<Transform>();
var transform = gameObject.transform;
children.Push(transform);
if (gameObject.transform.childCount == 0)
{
return;
}
var children = new Stack<Transform>(gameObject.transform.Cast<Transform>());
while (children.Count > 0)
{
Transform child = children.Pop();
int childCount = child.childCount;
child.gameObject.layer = layer;
if (child.childCount <= 0)
if (childCount <= 0)
{
continue;
}
foreach (Transform grandChild in child)
for (var childIndex = 0; childIndex < childCount; childIndex++)
{
children.Push(grandChild);
children.Push(child.GetChild(childIndex));
}
}
}