22// Copyright (c) Unity Technologies. For terms of use, see
33// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
44
5+ using System ;
6+ using System . Linq ;
57using System . Collections . Generic ;
68using UnityEngine ;
7-
9+ using Object = UnityEngine . Object ;
810
911namespace UnityEditor
1012{
@@ -27,33 +29,39 @@ public static string[] FindAssets(string filter, string[] searchInFolders)
2729
2830 private static string [ ] FindAssets ( SearchFilter searchFilter )
2931 {
30- if ( searchFilter . folders != null && searchFilter . folders . Length > 0 )
31- return SearchInFolders ( searchFilter ) ;
32- return SearchAllAssets ( searchFilter ) ;
32+ return FindAllAssets ( searchFilter ) . Select ( property => property . guid ) . ToArray ( ) ;
3333 }
3434
35- private static string [ ] SearchAllAssets ( SearchFilter searchFilter )
35+ internal static IEnumerable < HierarchyProperty > FindAllAssets ( SearchFilter searchFilter )
3636 {
37- var property = new HierarchyProperty ( HierarchyType . Assets ) ;
38- property . SetSearchFilter ( searchFilter ) ;
39- property . Reset ( ) ;
40- var guids = new List < string > ( ) ;
41- while ( property . Next ( null ) )
42- {
43- guids . Add ( property . guid ) ;
44- }
45- return guids . ToArray ( ) ;
37+ var enumerator = EnumerateAllAssets ( searchFilter ) ;
38+ while ( enumerator . MoveNext ( ) )
39+ yield return enumerator . Current ;
40+ }
41+
42+ internal static IEnumerator < HierarchyProperty > EnumerateAllAssets ( SearchFilter searchFilter )
43+ {
44+ if ( searchFilter . folders != null && searchFilter . folders . Length > 0 )
45+ return FindInFolders ( searchFilter , p => p ) ;
46+
47+ return FindEverywhere ( searchFilter , p => p ) ;
4648 }
4749
48- private static string [ ] SearchInFolders ( SearchFilter searchFilter )
50+ private static IEnumerator < T > FindInFolders < T > ( SearchFilter searchFilter , Func < HierarchyProperty , T > selector )
4951 {
50- var property = new HierarchyProperty ( HierarchyType . Assets ) ;
51- var guids = new List < string > ( ) ;
5252 foreach ( string folderPath in searchFilter . folders )
5353 {
54+ var folderInstanceID = AssetDatabase . GetMainAssetOrInProgressProxyInstanceID ( folderPath ) ;
55+ var rootPath = "Assets" ;
56+
57+ var pathComponents = folderPath . Split ( '/' ) ;
58+ // Find the right rootPath if folderPath is part of a package
59+ if ( pathComponents . Length > 1 && pathComponents [ 0 ] == UnityEditor . PackageManager . Folders . GetPackagesMountPoint ( ) )
60+ rootPath = pathComponents [ 0 ] + "/" + pathComponents [ 1 ] ;
61+
5462 // Set empty filter to ensure we search all assets to find folder
63+ var property = new HierarchyProperty ( rootPath ) ;
5564 property . SetSearchFilter ( new SearchFilter ( ) ) ;
56- int folderInstanceID = GetMainAssetInstanceID ( folderPath ) ;
5765 if ( property . Find ( folderInstanceID , null ) )
5866 {
5967 // Set filter after we found the folder
@@ -62,15 +70,30 @@ private static string[] SearchInFolders(SearchFilter searchFilter)
6270 int [ ] expanded = null ; // enter all children of folder
6371 while ( property . NextWithDepthCheck ( expanded , folderDepth + 1 ) )
6472 {
65- guids . Add ( property . guid ) ;
73+ yield return selector ( property ) ;
6674 }
6775 }
6876 else
6977 {
7078 Debug . LogWarning ( "AssetDatabase.FindAssets: Folder not found: '" + folderPath + "'" ) ;
7179 }
7280 }
73- return guids . ToArray ( ) ;
81+ }
82+
83+ private static IEnumerator < T > FindEverywhere < T > ( SearchFilter searchFilter , Func < HierarchyProperty , T > selector )
84+ {
85+ var rootPaths = new List < string > ( ) ;
86+ rootPaths . Add ( "Assets" ) ;
87+ rootPaths . AddRange ( UnityEditor . PackageManager . Folders . GetPackagesPaths ( ) ) ;
88+ foreach ( var rootPath in rootPaths )
89+ {
90+ var property = new HierarchyProperty ( rootPath ) ;
91+ property . SetSearchFilter ( searchFilter ) ;
92+ while ( property . Next ( null ) )
93+ {
94+ yield return selector ( property ) ;
95+ }
96+ }
7497 }
7598 }
7699}
0 commit comments