From d17e1670de0b38c9efe5724c2c37ac511ca3b8ff Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 9 May 2022 19:47:02 +0100 Subject: [PATCH] Remove call to Cast, avoid alloc of Transform.Enumerator Children are now retrieved using a combination of childCount and GetChild method --- X10D.Unity/src/GameObjectExtensions.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/X10D.Unity/src/GameObjectExtensions.cs b/X10D.Unity/src/GameObjectExtensions.cs index 8d2b9d8..97d03d3 100644 --- a/X10D.Unity/src/GameObjectExtensions.cs +++ b/X10D.Unity/src/GameObjectExtensions.cs @@ -184,27 +184,25 @@ public static class GameObjectExtensions throw new ArgumentNullException(nameof(gameObject)); } - gameObject.layer = layer; + var children = new Stack(); + var transform = gameObject.transform; + children.Push(transform); - if (gameObject.transform.childCount == 0) - { - return; - } - - var children = new Stack(gameObject.transform.Cast()); 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)); } } }