Created
February 26, 2019 10:29
-
-
Save elmirjagudin/c6fd1c62138d901549cd045b8b8fefec to your computer and use it in GitHub Desktop.
An example how to iterate over all Unity GameObjects in a hierarchy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// | |
/// an example how to iterate over all GameObjects in a hierarchy | |
/// | |
class GameObjectsIterator | |
{ | |
/// | |
/// an example how to print root GameObject and all it's | |
/// children to debug console | |
/// | |
public static void PrintAllObjects(GameObject root) | |
{ | |
foreach (var go in AllObjects(root)) | |
{ | |
Debug.LogFormat("GameObject {0}", go); | |
} | |
} | |
/// | |
/// makes an enumarable that contains all game objects starting with root | |
/// | |
static IEnumerable<GameObject>AllObjects(GameObject root) | |
{ | |
yield return root; | |
foreach (Transform ch in root.transform) | |
{ | |
foreach (var go in AllObjects(ch.gameObject)) | |
{ | |
yield return go; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment