Hello, I’m working on an editor creating “cache” files to kelp my editor tools to keep track of changes made to certain assets between hot-reloading segments. For now, these cached files are stored in the Project folder as ScriptableObjects. The reason why I’m not using a standard file format like JSON is because these scriptableObjects can hold references to other Unity assets inside the Project folder, and storing them inside the JSON file would be impossible (If you know of a better method for storing them, please let me know.)
So, with those ScriptableObjects, I now want to exclude them from the Search window, as the user doesn’t need to directly interact with them. Looking at the official documentation for Unity Search it seems I can create a custom search provider for filtering results, but there doesn’t seem to be a way to exclude some asset types by default. Is there a way to do so?
Hi @Quasar47,
You can open the Index Manager : Menu Window → Search → Index Manager.
And modify the Exclude list. You can exclude assets by extension or folders.
I hope this resolve your issue.
Seb
1 Like
Thank you, this is exactly what I was looking for!
I’ve poked around in the UnityEditor.Search namespace to find any class or method that would allow me to do the same via code (so that I could do it automatically when the editor opens), but I couldn’t find anything. Do you have any pointers towards how I could accomplish this?
Hi @Quasar47,
- We do not have code API to edit the indexing settings. But if you generate your files in a specific folder you shouldn’t have to modify the settings often? What do you want to do each time the editor open?
- Alternatively if you want asset to be indexed but if you do not want some specific results part of a query you can use the “Exclude operator” :
-
Ex: let’s say I want all materials except those in my Prototype folder. I can write the following query:
p: t:material -dir:prototype
Good luck,
Seb
Hello, the reason I’m doing this is because I’m working on a package destined to be used by other developers. Ideally, they should not have to access the cache files to ensure the package functions properly, hence why I wanted to ‘hide’ them, if such a thing is possible.
I wanted to add the exclusion automatically to the Index Manager once the user downloads the package. To do so, I discovered the Search.index file was located in the UserSettings folder. So I made a static class that automatically handles adding that exclusion to the JSON file when the editor opens.
/// <summary>
/// Used to exclude folders from the Unity Search window
/// the user is not supposed to access
/// </summary>
[InitializeOnLoad]
internal static class UnitySearchExclusion
{
#region Internal static methods
/// <summary>
/// Constructor
/// </summary>
static UnitySearchExclusion()
{
// Run only once per session
if (!SessionState.GetBool("FirstInitDone", false))
{
// We retrieve the Search.index file
// and make sure we assign the _cache folder path to its 'excludes' list
// in order to prevent the user from accessing it
string searchIndexPath = $"{Application.dataPath}/../UserSettings/Search.index";
string json = File.ReadAllText(searchIndexPath);
UserSettingsSearchIndexDTO searchIndex = JsonUtility.FromJson<UserSettingsSearchIndexDTO>(json);
searchIndex.excludes ??= new List<string>();
if (!searchIndex.excludes.Contains(EditorConstants.CACHE_FOLDER_PATH))
{
searchIndex.excludes.Add(EditorConstants.CACHE_FOLDER_PATH);
}
File.WriteAllText(searchIndexPath, JsonUtility.ToJson(searchIndex, true));
SessionState.SetBool("FirstInitDone", true);
}
}
#endregion
Thanks for the clarifications @Quasar47, I now properly understand your use case.
Like you discover, index settings are simple json file. We do not have API to formally modify those, but as you prove it is easy enough to do 
1 Like