/// <summary>
 /// Collects the instance ids and indices of all objects in the hierarchy below a set of root objects (including
 /// the roots).
 /// </summary>
 /// <param name="hierarchy">The hierarchy to operate on.</param>
 /// <param name="instanceIds">The instance ids of the root objects, but will also be filled with the instance
 /// ids of all objects that were visited.</param>
 /// <param name="visitedIndices">A hashmap that is used to output the visited indices. A value maps to true if
 /// it was a root, false otherwise.</param>
 /// <param name="dependency">The dependency for the job.</param>
 /// <returns>A job handle representing the job.</returns>
 public static JobHandle CollectHierarchyInstanceIdsAndIndicesAsync(this SceneHierarchy hierarchy, NativeList <int> instanceIds, NativeHashMap <int, bool> visitedIndices, JobHandle dependency = default)
 {
     return(new CollectHierarchyInstanceIdsAndIndicesJob
     {
         Hierarchy = hierarchy,
         VisitedInstanceIds = instanceIds,
         VisitedIndices = visitedIndices
     }.Schedule(dependency));
 }
 /// <summary>
 /// Collects the instance ids of all objects in the hierarchy below a set of root objects (including the roots).
 /// </summary>
 /// <param name="hierarchy">The hierarchy to operate on.</param>
 /// <param name="rootInstanceIds">The instance ids of the root objects.</param>
 /// <param name="visitedInstanceIds">A hashset that is used to output the collected instance ids.</param>
 /// <param name="dependency">The dependency for the job.</param>
 /// <returns>A job handle representing the job.</returns>
 public static JobHandle CollectHierarchyInstanceIdsAsync(this SceneHierarchy hierarchy, NativeArray <int> rootInstanceIds, NativeHashSet <int> visitedInstanceIds, JobHandle dependency = default)
 {
     return(new CollectHierarchyInstanceIdsJob
     {
         Hierarchy = hierarchy,
         Roots = rootInstanceIds,
         VisitedInstances = visitedInstanceIds
     }.Schedule(dependency));
 }
        static void CollectHierarchyInstanceIdsImpl(SceneHierarchy hierarchy, NativeArray <int> rootInstanceIds, NativeHashSet <int> visitedInstanceIds)
        {
            var openIndices = new NativeList <int>(0, Allocator.Temp);

            for (int i = 0; i < rootInstanceIds.Length; i++)
            {
                if (hierarchy.TryGetIndexForInstanceId(rootInstanceIds[i], out int idx))
                {
                    openIndices.Add(idx);
                }
            }

            while (openIndices.Length > 0)
            {
                int idx = openIndices[openIndices.Length - 1];
                openIndices.Length--;
                visitedInstanceIds.Add(hierarchy.GetInstanceIdForIndex(idx));
                var iter = hierarchy.GetChildIndicesForIndex(idx);
                while (iter.MoveNext())
                {
                    openIndices.Add(iter.Current);
                }
            }
        }
 /// <summary>
 /// Collects the instance ids of all objects in the hierarchy below a set of root objects.
 /// </summary>
 /// <param name="hierarchy">The hierarchy to operate on.</param>
 /// <param name="rootInstanceIds">The instance ids of the root objects.</param>
 /// <param name="visitedInstanceIds">A hashset that is used to output the collected instance ids.</param>
 public static void CollectHierarchyInstanceIds(this SceneHierarchy hierarchy, NativeArray <int> rootInstanceIds,
                                                NativeHashSet <int> visitedInstanceIds)
 {
     CollectHierarchyInstanceIdsImpl(hierarchy, rootInstanceIds, visitedInstanceIds);
 }