/// <summary>
 /// Get the direct children of a <see cref="Hierarchy"/> filtered by type and an optional filter delegate. It stores them in a List parameter
 /// to avoid garbage allocations.
 /// </summary>
 /// <typeparam name="TElement">The type of the child elements</typeparam>
 /// <param name="hierarchy">The parent hierarchy</param>
 /// <param name="results">A List to store the results</param>
 /// <param name="filter">An optional filter callback</param>
 public static void GetChildren <TElement>(this Hierarchy hierarchy, List <TElement> results, Func <TElement, bool> filter = null)
     where TElement : VisualElement
 {
     for (int i = 0; i < hierarchy.childCount; i++)
     {
         if (hierarchy[i] is TElement element && (filter == null || filter(element)))
         {
             results.Add(element);
         }
     }
 }
 /// <summary>
 /// Execute an action on all direct children with a certain type.
 /// </summary>
 /// <typeparam name="TElement">The type of the child elements.</typeparam>
 /// <param name="hierarchy">The parent hierarchy.</param>
 /// <param name="action">The action to execute.</param>
 public static void ForEachChild <TElement>(this Hierarchy hierarchy, Action <TElement> action)
     where TElement : VisualElement
 {
     for (int i = 0; i < hierarchy.childCount; i++)
     {
         if (hierarchy[i] is TElement element)
         {
             action(element);
         }
     }
 }
        /// <summary>
        /// Get the first direct child with a certain type that passes an optional filter delegate.
        /// </summary>
        /// <typeparam name="TElement">The type of the child</typeparam>
        /// <param name="hierarchy">The parent element</param>
        /// <param name="filter">An optional filter callback</param>
        /// <returns>A child that satisfies conditions or null.</returns>
        public static TElement GetFirstChild <TElement>(this Hierarchy hierarchy, Func <TElement, bool> filter = null)
            where TElement : VisualElement
        {
            for (int i = 0; i < hierarchy.childCount; i++)
            {
                if (hierarchy[i] is TElement element && (filter == null || filter(element)))
                {
                    return(element);
                }
            }

            return(null);
        }