Super-charging Object Selection & Searching in Unity

Juris
6 min readSep 5, 2023

--

The Scenario

As many of us do, I have a large Unity project with many different packages imported along with their assets, textures and materials. As a result, I always have difficulty finding the asset I need using the basic Object Selector, as it typically shows a cluttered list of assets and materials in the project, many unrelated to what I’m looking for.

A screenshot of the selection window for a material in the Unity Editor
The default material selection window.

Granted, I could start typing in the search field and narrow down my options, but doing this for every material I needed to find and assign would quickly become cumbersome. I could also change the view by zooming in, but this doesn’t help with filtering. Thankfully, Unity provides a better, built-in solution — the Search window.

The Search Window

The Search window in Unity is a powerful tool that can be used to find assets, objects, and other items in your project. It can be opened by pressing Ctrl+K or by going to Edit > Search All

The Search window is split into five distinct sections:

  1. The Search Field — A place to search for your asset via plain text or query.
  2. Saved Searches — Access to your saved searches across User and Project.
  3. Results Area — This area displays your search results.
  4. Search Provider — Enables you to filter the results based on the Project, Hierarchy, Menus, Settings or All.
  5. Preview Inspector — Provides a view of the selected item’s properties.
A labelled image of the Search Window in the Unity Editor, with numbers from 1 through 5
An image of the Search Window, with the search for a “Grid” file.

Regular and Special Searches

The Search window already works great for basic search queries, such as the names of the items we’re after. But you can power up this functionality using search tokens.

A search token is a text string that you can use in the search field to search using only a specific Search Provider.

There are several regular search tokens available:

  • Project: Searches Project Assets. The search token is p:. For example, p:Player will search for Assets that match the term "Player".
  • Hierarchy: Searches GameObjects in the Scene. The search token is h:. For example, h:Main Camera will search the current Scene for GameObjects that match the term "Main Camera".
  • Settings: Searches all Project Settings and Preferences. The search token is set:. For example, set:VFX will find Project Settings and Preferences pages that match the term "VFX".
  • Menus: Searches the Unity main menu. The search token is m:. For example, m:TextMesh Pro will search the Unity main menu for commands that contain "TextMesh Pro".

These search tokens already improve our searching capabilities, but we can take it a step further by using the special Search Providers, along with their search tokens:

  • Help: Searches the Quick Search help. The search token is ?. For example, ?asset will search for Search help entries containing the word "Asset".
  • Calculator: Computes mathematical expressions. The search token is =. For example, =2*3+29/2 calculates the answer to the expression 2*3+29/2.
  • Files: Searches for files. The search token is find:. For example, find:Paint Mat searches for all assets paths containing the words paint AND the word mat (for example: PaintBrush_Mat.mat, DryWallPainted_Mat.mat).
  • Static API: Finds and executes static API methods. The search token is #. For example, #Mesh searches for static API methods with “Mesh” in their names.
  • Packages: Searches the Unity package database. The search token is pkg:. For example, pkg:vector searches the Unity package database for packages that match the term “vector”.
  • Asset Store: Searches the Unity Asset Store. The search token is store:. For example, store:texture searches the Unity Asset Store for Assets that match the term “texture”.
  • Saved Queries: Searches for saved queries. The search token is q:. For example, q:enemies searches all saved searches containing the word “enemies”.
  • Logs: Searches the Editor.log file. The search token is log:. For example, log:cache searches the Editor.log file for information that matches “cache”.

Combining the different search tokens allows us to quickly and easily find the assets, objects, and other items we need in our Unity projects. Let's spice things up by changing the default window that opens when using the object selector from the boring old search to something more exciting!

Replacing the Object Selector

A lesser-known feature of Unity is that you can use the more advanced Object Picker window instead of the default Object Selector. To do so, navigate to: Edit > Preferences > Search > Search Engines > Object Selector and select the advanced option.

A screenshot of the Unity Editor preferences window, focused on the “Search” section under “Search Engines”
The option to select the “advanced” object selector is under Preferences > Search > “Search Engines”

Selecting the "Advanced" Object Selector will open the window for all selections in the editor, providing you with more control and a better searching experience.

Great! Now, we can make the most of search tokens across all our exposed properties in the editor.

The SearchContext Attribute

After making the above changes, our object fields will open a more advanced search window. However, we can enhance this feature further by specifying additional search context to our fields using theSearchContext attribute.

This (SearchContext) attribute can be attached to a component object field in order to have the ObjectField use the advanced Object Picker.

It’s constructed with the following properties:

  • The query — The core search string to open the window
  • The flags — Optional search view flags for the window (e.g. GridView , ListView )
  • Provider ID’s — An optional comma-separated list list of Search Provider IDs used to create the search context (e.g. asset , scene )
  • Instantiable Providers — Concrete types assigned to the Object Picker search context.

Several examples are available in the Unity Documentation, but here’s a basic one based on my scenario. I want to filter my search only to include Material types that include the textGrid somewhere.

[SearchContext("t:Material Grid")]
Unity Search Window with a filter for Materials including “Grid”

This works! But it’d be nicer if we could get a Grid view by default to visualise these materials. This is where we can use SearchViewFlags, specifically the GridView flag.

[SearchContext("t:Material Grid", SearchViewFlags.GridView)]
A filtered Search Window in the Unity Editor, with a grid view.

We now have a Grid View of all the materials containing the string “Grid”. We can take this a lot further, but for the scope of this tutorial, this already gives us a much more friendlier interface to work with than the default search window.

Summary

To summarise the information covered:

  • The Search window in Unity is a powerful tool that can be used to find assets, objects, and other items in your project.
  • Search tokens are special keywords that can narrow down the search results. For example, the p: token can be used to search for project assets.
  • Special search providers can be used to search for specific types of items. For example, the ? token can be used to search for Quick Search help entries, and the = token can be used to calculate mathematical expressions.
  • You can also use the SearchContext attribute to specify additional search context to your fields. This can filter the results by type, provider, or custom query.

If you found this helpful, don’t forget to spread the word! Share this article with your friends and colleagues who use Unity.

--

--