static bool AnyTargetMaterialHasChildren(string[] targetPaths)
        {
            GUID[] guids = targetPaths.Select(path => AssetDatabase.GUIDFromAssetPath(path)).ToArray();

            Func <string, bool> HasChildrenInPath = (string rootPath) => {
                var property = new HierarchyProperty(rootPath, false);
                property.SetSearchFilter(new SearchFilter {
                    classNames = new string[] { "Material" }, searchArea = SearchFilter.SearchArea.AllAssets
                });
                while (property.Next(null))
                {
                    GUID parent;
                    var  child = InternalEditorUtility.GetLoadedObjectFromInstanceID(property.GetInstanceIDIfImported()) as Material;
                    if (child)
                    {
                        if (AssetDatabase.IsForeignAsset(child))
                        {
                            continue;
                        }
                        parent = AssetDatabase.GUIDFromAssetPath(AssetDatabase.GetAssetPath(child.parent));
                    }
                    else
                    {
                        var path = AssetDatabase.GUIDToAssetPath(property.guid);
                        if (!path.EndsWith(".mat", StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                        parent = EditorMaterialUtility.GetMaterialParentFromFile(path);
                    }

                    for (int i = 0; i < guids.Length; i++)
                    {
                        if (guids[i] == parent)
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            };

            if (HasChildrenInPath("Assets"))
            {
                return(true);
            }
            foreach (var package in PackageManagerUtilityInternal.GetAllVisiblePackages(false))
            {
                if (package.source == PackageManager.PackageSource.Local && HasChildrenInPath(package.assetPath))
                {
                    return(true);
                }
            }
            return(false);
        }
        void Search()
        {
            var newResults = new List <int>();

            var maxAddCount = k_MaxSearchIterationPerFrame;

            while (--maxAddCount >= 0)
            {
                if (!enumerator.MoveNext())
                {
                    enumerator = null;
                    break;
                }
                var child = InternalEditorUtility.GetLoadedObjectFromInstanceID(enumerator.Current.GetInstanceIDIfImported()) as Material;
                if (!child)
                {
                    // First check guid from file to avoid loading material in memory
                    string path = AssetDatabase.GUIDToAssetPath(enumerator.Current.guid);
                    if (EditorMaterialUtility.GetMaterialParentFromFile(path) != targetGUID)
                    {
                        continue;
                    }
                    child = AssetDatabase.LoadAssetAtPath <Material>(path);
                }
                if (child != null && child.parent == target)
                {
                    newResults.Add(child.GetInstanceID());
                }
            }

            int newElements = newResults.Count;
            int i           = results.Length;

            System.Array.Resize(ref results, results.Length + newElements);
            for (var j = 0; j < newElements && i < results.Length; ++j, ++i)
            {
                results[i] = newResults[j];
            }

            listArea.ShowObjectsInList(results);
        }
        static void ReparentMaterialChildren(string assetPath)
        {
            var toDelete     = AssetDatabase.LoadAssetAtPath <Material>(assetPath);
            var toDeleteGUID = AssetDatabase.GUIDFromAssetPath(assetPath);
            var newParent    = toDelete.parent;

            Action <string> ReparentInPath = (string rootPath) => {
                var property = new HierarchyProperty(rootPath, false);
                property.SetSearchFilter(new SearchFilter {
                    classNames = new string[] { "Material" }, searchArea = SearchFilter.SearchArea.AllAssets
                });
                while (property.Next(null))
                {
                    var child = InternalEditorUtility.GetLoadedObjectFromInstanceID(property.GetInstanceIDIfImported()) as Material;
                    if (!child)
                    {
                        // First check guid from file to avoid loading all materials in memory
                        string path = AssetDatabase.GUIDToAssetPath(property.guid);
                        if (EditorMaterialUtility.GetMaterialParentFromFile(path) != toDeleteGUID)
                        {
                            continue;
                        }
                        child = AssetDatabase.LoadAssetAtPath <Material>(path);
                    }
                    if (child != null && child.parent == toDelete && !AssetDatabase.IsForeignAsset(child))
                    {
                        child.parent = newParent;
                    }
                }
            };

            ReparentInPath("Assets");
            foreach (var package in PackageManagerUtilityInternal.GetAllVisiblePackages(false))
            {
                if (package.source == PackageManager.PackageSource.Local)
                {
                    ReparentInPath(package.assetPath);
                }
            }
        }