Example and Documentation done by myself, code was written by the help of Chat-GPT, then I modified it.
namespace User.Defined
{
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
///
/// A providing extension methods to enhance
/// one's experience in Unity GameDev projects.
///
internal static class CustomExtensionMethods
{
///
/// Retrieves every single in a given .
///
///
/// This parameter is the current instance.
///
///
/// A comprehending all
/// s available in the in question.
///
internal static GameObject[] GetAllGameObjectsInHierarchy(this Scene self)
{
var roots = self.GetRootGameObjects();
var transList = new List();
for (var i = 0; i < roots.Length; i++)
transList.AddRange(roots[i].GetSelfAndDescendants());
return transList.ToArray();
}
///
/// Finds each in a given root.
///
///
/// Note: this method does not only get any possible parent, but also all of its children
///
and if a given child found to be a parent itself, the loop goes on... ///
/// For further details, check the following link out:
///
///
///
///
/// The originating source currently using the method.
///
///
/// An of any on this
/// .
///
internal static GameObject[] GetSelfAndDescendants(this GameObject self)
{
var transforms = self.GetComponentsInChildren(includeInactive: true);
var gameObjectList = new List(transforms.Length);
for (var i = 0; i < transforms.Length; i++)
gameObjectList.Add(transforms[i].gameObject);
return gameObjectList.ToArray();
}
}
}
Usage example:
using UnityEngine;
using UnityEngine.SceneManagement;
using User.Defined;
// NOTE: This shoud be attached to a GameObject as a C# file named: ShowCase
internal class ShowCase : MonoBehaviour
{
[SerializeField]
private bool entireScene = true;
private void Awake()
{
if (entireScene) EntireSceneScoped();
else GameObjectScoped();
}
private void EntireSceneScoped()
{
foreach (var gameObject in SceneManager.GetActiveScene().GetAllGameObjectsInHierarchy())
// TODO: Your own code here instead...
print(gameObject.name);
}
private void GameObjectScoped()
{
foreach (var gameObject in gameObject.GetSelfAndDescendants())
// TODO: Your own code here instead...
print(gameObject.name);
}
}
and if a given child found to be a parent itself, the loop goes on... ///