Beispiel #1
0
 private static void TrackAssetIndexChanges(string[] updated, string[] deleted, string[] moved)
 {
     if (updated.Concat(deleted).Any(u => u.EndsWith(".index", StringComparison.OrdinalIgnoreCase)))
     {
         assetIndexes = ADBIndex.Enumerate().ToList();
     }
 }
 private static void SetupIndexers()
 {
     if (SearchSettings.assetIndexing == SearchAssetIndexing.Files && fileIndexer == null)
     {
         var packageRoots = Utils.GetPackagesPaths().Select(p => new SearchIndexerRoot(Path.GetFullPath(p).Replace('\\', '/'), p));
         var roots = new[] { new SearchIndexerRoot(Application.dataPath, "Assets") }.Concat(packageRoots);
         fileIndexer = new FileSearchIndexer(type, roots);
         fileIndexer.Build();
     }
     else if (SearchSettings.assetIndexing == SearchAssetIndexing.Complete && assetIndexes == null)
     {
         assetIndexes = ADBIndex.Enumerate().ToList();
         foreach (var db in assetIndexes)
         {
             db.IncrementalUpdate();
         }
         AssetPostprocessorIndexer.contentRefreshed += TrackAssetIndexChanges;
     }
 }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var jsonText = System.IO.File.ReadAllText(ctx.assetPath);
            var settings = JsonUtility.FromJson <AssetIndexerSettings>(jsonText);
            var fileName = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath);

            db           = ScriptableObject.CreateInstance <ADBIndex>();
            db.name      = fileName;
            db.hideFlags = HideFlags.NotEditable;
            db.settings  = settings;
            db.index     = new AssetIndexer(db.name, settings);

            if (!Reimport(ctx))
            {
                Build();
            }

            EditorApplication.delayCall -= Cleanup;
            EditorApplication.delayCall += Cleanup;

            ctx.AddObjectToAsset(fileName, db);
            ctx.SetMainObject(db);
        }
        private static IEnumerator SearchAssets(SearchContext context, List <SearchItem> items, SearchProvider provider)
        {
            var searchQuery    = context.searchQuery;
            var searchGuids    = context.categories.Any(c => c.id == "guid" && c.isEnabled);
            var searchPackages = context.categories.Any(c => c.id == "packages" && c.isEnabled);

            // Search by GUIDs
            if (searchGuids)
            {
                var gui2Path = AssetDatabase.GUIDToAssetPath(searchQuery);
                if (!String.IsNullOrEmpty(gui2Path))
                {
                    yield return(provider.CreateItem(gui2Path, -1, $"{Path.GetFileName(gui2Path)} ({searchQuery})", null, null, null));
                }
            }

            if (SearchSettings.useUberIndexing)
            {
                var adbIndex = ADBIndex.Get();

                if (!adbIndex.IsReady())
                {
                    foreach (var assetEntry in AssetDatabase.FindAssets(searchQuery)
                             .Select(AssetDatabase.GUIDToAssetPath)
                             .Select(path => provider.CreateItem(path, Path.GetFileName(path))))
                    {
                        yield return(assetEntry);
                    }
                }

                // Search index
                while (!adbIndex.IsReady())
                {
                    yield return(null);
                }

                yield return(adbIndex.Search(searchQuery).Select(e =>
                {
                    var filename = Path.GetFileName(e.path);
                    var filenameNoExt = Path.GetFileNameWithoutExtension(e.path);
                    var itemScore = e.score;
                    var words = context.searchPhrase;
                    if (filenameNoExt.Equals(words, StringComparison.OrdinalIgnoreCase))
                    {
                        itemScore = SearchProvider.k_RecentUserScore + 1;
                    }

                    string description = adbIndex.GetDebugIndexStrings(e.path);
                    return provider.CreateItem(e.path, itemScore, filename, description, null, null);
                }));
            }
            else
            {
                var fileIndexerReady = fileIndexer.IsReady();
                if (fileIndexerReady)
                {
                    if (searchQuery.IndexOfAny(k_InvalidIndexedChars) == -1)
                    {
                        foreach (var item in SearchFileIndex(searchQuery, searchPackages, provider))
                        {
                            yield return(item);
                        }
                        if (!context.wantsMore)
                        {
                            yield break;
                        }
                    }
                }

                if (!searchPackages)
                {
                    if (!searchQuery.Contains("a:assets"))
                    {
                        searchQuery = "a:assets " + searchQuery;
                    }
                }

                foreach (var assetEntry in AssetDatabase.FindAssets(searchQuery).Select(AssetDatabase.GUIDToAssetPath).Select(path => provider.CreateItem(path, Path.GetFileName(path))))
                {
                    yield return(assetEntry);
                }

                if (!fileIndexerReady)
                {
                    // Indicate to the user that we are still building the index.
                    while (!fileIndexer.IsReady())
                    {
                        yield return(null);
                    }

                    foreach (var item in SearchFileIndex(searchQuery, searchPackages, provider))
                    {
                        yield return(item);
                    }
                }
            }

            // Search file system wildcards
            if (context.searchQuery.Contains('*'))
            {
                var safeFilter   = string.Join("_", context.searchQuery.Split(k_InvalidSearchFileChars));
                var projectFiles = System.IO.Directory.EnumerateFiles(Application.dataPath, safeFilter, System.IO.SearchOption.AllDirectories);
                projectFiles = projectFiles.Select(path => path.Replace(Application.dataPath, "Assets").Replace("\\", "/"));
                foreach (var fileEntry in projectFiles.Select(path => provider.CreateItem(path, Path.GetFileName(path))))
                {
                    yield return(fileEntry);
                }
            }
        }
Beispiel #5
0
 internal void OnEnable()
 {
     m_DB              = (ADBIndex)target;
     m_Settings        = serializedObject.FindProperty("settings");
     m_IndexTitleLabel = new GUIContent($"{m_DB.index?.name ?? m_DB.name} ({EditorUtility.FormatBytes(m_DB.bytes?.Length ?? 0)})");
 }
Beispiel #6
0
        internal static SearchProvider CreateProvider()
        {
            return(new SearchProvider(type, displayName)
            {
                priority = 25,
                filterId = "p:",
                showDetails = SearchSettings.fetchPreview,

                onEnable = () =>
                {
                    if (SearchSettings.assetIndexing == SearchAssetIndexing.Files && fileIndexer == null)
                    {
                        var packageRoots = Utils.GetPackagesPaths().Select(p => new SearchIndexerRoot(Path.GetFullPath(p).Replace('\\', '/'), p));
                        var roots = new[] { new SearchIndexerRoot(Application.dataPath, "Assets") }.Concat(packageRoots);
                        fileIndexer = new FileSearchIndexer(type, roots);
                        fileIndexer.Build();
                    }
                    else if (SearchSettings.assetIndexing == SearchAssetIndexing.Complete && assetIndexes == null)
                    {
                        assetIndexes = ADBIndex.Enumerate().ToList();
                        foreach (var db in assetIndexes)
                        {
                            db.IncrementalUpdate();
                        }
                        AssetPostprocessorIndexer.contentRefreshed += TrackAssetIndexChanges;
                    }
                },

                isEnabledForContextualSearch = () => Utils.IsFocusedWindowTypeName("ProjectBrowser"),

                toObject = (item, type) => AssetDatabase.LoadAssetAtPath(item.id, type),

                fetchItems = (context, items, provider) => SearchAssets(context, provider),

                fetchKeywords = (context, lastToken, items) =>
                {
                    if (!lastToken.Contains(":"))
                    {
                        return;
                    }
                    if (SearchSettings.assetIndexing == SearchAssetIndexing.Complete)
                    {
                        items.AddRange(assetIndexes.SelectMany(db => db.index.GetKeywords()));
                    }
                    else
                    {
                        items.AddRange(typeFilter.Select(t => "t:" + t));
                    }
                },

                fetchDescription = (item, context) => (item.description = GetAssetDescription(item.id)),
                fetchThumbnail = (item, context) => Utils.GetAssetThumbnailFromPath(item.id),
                fetchPreview = (item, context, size, options) => Utils.GetAssetPreviewFromPath(item.id, size, options),

                startDrag = (item, context) =>
                {
                    var obj = AssetDatabase.LoadAssetAtPath <Object>(item.id);
                    if (obj)
                    {
                        Utils.StartDrag(obj, item.label);
                    }
                },
                trackSelection = (item, context) => Utils.PingAsset(item.id)
            });
        }