Created
June 4, 2019 03:04
-
-
Save jschmidt42/0aa91bbb39f52fb5ba74ce75911769c6 to your computer and use it in GitHub Desktop.
ScriptableObjectSearchProvider Example for Unity Quick Search v1.2.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using JetBrains.Annotations; | |
using UnityEditor; | |
using UnityEngine; | |
using Object = UnityEngine.Object; | |
namespace Unity.QuickSearch | |
{ | |
namespace Providers | |
{ | |
[UsedImplicitly] | |
static class ScriptableObjectSearchProvider | |
{ | |
private const string type = "scriptable_object"; | |
private const string displayName = "Scriptable Objects"; | |
[UsedImplicitly, SearchItemProvider] | |
private static SearchProvider CreateProvider() | |
{ | |
var allScriptableObjects = AssetDatabase.FindAssets("a:assets t:ScriptableObject").Select(AssetDatabase.GUIDToAssetPath).ToArray(); | |
var allScriptableTypes = allScriptableObjects.Select(p => AssetDatabase.GetMainAssetTypeAtPath(p).FullName).ToArray(); | |
var fileIndexer = new SearchIndexer("ScriptableObjectsIndex") | |
{ | |
enumerateRootEntriesHandler = r => allScriptableObjects, | |
getEntryComponentsHandler = (e, i) => | |
{ | |
return SearchUtils.SplitEntryComponents(allScriptableTypes[i], SearchUtils.entrySeparators, 2, 8) | |
.Concat(SearchUtils.SplitFileEntryComponents(e, SearchUtils.entrySeparators, 2, 8)); | |
} | |
}; | |
return new SearchProvider(type, displayName) | |
{ | |
filterId = "so:", | |
fetchItems = (context, items, provider) => | |
{ | |
var filter = context.searchQuery; | |
if (fileIndexer.IsReady()) | |
{ | |
var matches = fileIndexer.Search(filter); | |
items.AddRange(matches.Select(e => provider.CreateItem( | |
e.path, e.score, $"{Path.GetFileName(e.path)} ({allScriptableTypes[e.index]})", null, null, null))); | |
} | |
else | |
{ | |
// Fallback to asset database API while index is being built. | |
filter = "a:assets t:ScriptableObject " + filter; | |
items.AddRange(AssetDatabase.FindAssets(filter) | |
.Select(AssetDatabase.GUIDToAssetPath) | |
.Take(202) | |
.Select(path => provider.CreateItem(path, Path.GetFileName(path)))); | |
} | |
}, | |
fetchDescription = (item, context) => | |
{ | |
long fileSize = new FileInfo(item.id).Length; | |
item.description = $"{item.id} ({EditorUtility.FormatBytes(fileSize)})"; | |
return item.description; | |
}, | |
fetchThumbnail = (item, context) => | |
{ | |
item.thumbnail = UnityEditorInternal.InternalEditorUtility.FindIconForFile(item.id); | |
return item.thumbnail; | |
} | |
}; | |
} | |
[UsedImplicitly, SearchActionsProvider] | |
private static IEnumerable<SearchAction> ActionHandlers() | |
{ | |
return new[] | |
{ | |
new SearchAction(type, "open", null, "Open asset...") | |
{ | |
handler = (item, context) => | |
{ | |
var asset = AssetDatabase.LoadAssetAtPath<Object>(item.id); | |
if (asset != null) | |
AssetDatabase.OpenAsset(asset); | |
} | |
} | |
}; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment