Assets/Watson/Scripts/Utilities/Utility.cs
/// <summary>
/// Finds the objects in children of parent object by name of child
/// </summary>
/// <returns>The objects.</returns>
/// <param name="parent">Parent.</param>
/// <param name="nameChild">Name child.</param>
/// <param name="isContains">Check string contains the name instead of equality.</param>
/// <param name="sortByName">If true, children will be returned sorted by their name.</param>
Finds the objects in children of parent object by name of child
Finds the objects in children of parent object by name of child
public static T[] FindObjects<T>(GameObject parent, string nameChild, bool isContains = false, bool sortByName = false, bool includeInactive = true) where T : Component
        {
            T[] childObjects = null;
            List<T> listGameObject = new List<T>();
            string[] childPath = nameChild.Split('/');
            for (int i = 0; i < childPath.Length; i++)
            {
                string childTransformName = childPath[i];
				T[] childerenTransform = parent.GetComponentsInChildren<T>(includeInactive: includeInactive);
                if (childerenTransform != null)
                {
                    foreach (T item in childerenTransform)
                    {
                        if ((isContains && item.name.Contains(childTransformName)) || string.Equals(item.name, childTransformName))
                        {
                            if (i == childPath.Length - 1)
                            {
                                listGameObject.Add(item);
                            }
                            else
                            {
                                parent = item.gameObject;
                            }
                        }
                    }
                }
            }
            if (listGameObject.Count > 0)
            {
                if (sortByName)
                {
                    listGameObject.Sort((x, y) => x.name.CompareTo(y.name));
                }
                childObjects = listGameObject.ToArray();
            }
            return childObjects;
        }
public static T [ ] FindObjects < T > ( GameObject parent , string nameChild , bool isContains = false , bool sortByName = false , bool includeInactive = true ) where T : Component { T [ ] childObjects = null ; List < T > listGameObject = new List < T > ( ) ; string [ ] childPath = nameChild . Split ( ' ' ) ; for ( int i = 0 ; i < childPath . Length ; i ++ ) { string childTransformName = childPath [ i ] ; T [ ] childerenTransform = parent . GetComponentsInChildren < T > ( includeInactive : includeInactive ) ; if ( childerenTransform != null ) { foreach ( T item in childerenTransform ) { if ( ( isContains && item . name . Contains ( childTransformName ) ) || string . Equals ( item . name , childTransformName ) ) { if ( i == childPath . Length - 1 ) { listGameObject . Add ( item ) ; } else { parent = item . gameObject ; } } } } } if ( listGameObject . Count > 0 ) { if ( sortByName ) { listGameObject . Sort ( ( x , y ) => x . name . CompareTo ( y . name ) ) ; } childObjects = listGameObject . ToArray ( ) ; } return childObjects ; }
Finds the objects in children of parent object by name of child
Finds the objects in children of parent object by name of child
None
parent
GameObject
None
nameChild
string
None
isContains
bool
Check string contains the name instead of equality.
Check string contains the name instead of equality .
sortByName
bool
If true, children will be returned sorted by their name.
If true children will be returned sorted by their name .
includeInactive
bool