# Define a context for the Advanced Object Picker

> Use search queries to filter the objects the Advanced Object Picker shows users.

To help users populate objects in a component, define a context for the search in the **Advanced Object Picker**. This makes it easier to construct the rest of the search query, and might even narrow the results enough that the user doesn't need to perform a query. For example, if the object is a material, define a context that filters the **Advanced Object Picker** to show only materials, or specific categories of material.

> **Tip:**
>
> The filter is a standard search query, so the user can remove or change it to change their search context.

## Define the context

To define the context, in the component's script:

1. For the field that requires object selection, use the [`SearchContextAttribute`](/engine/6000.5/script-reference/unityengine/search/searchcontextattribute.md) to set the object selection to **Advanced Object Picker**.
2. Create a query to filter the objects the **Advanced Object Picker** lists.

For example, this query focuses the **Advanced Object Picker** on the Asset search provider:

```
// You can specify an initial query, a search provider and some SearchViewFlags
[SearchContext("t:texture filtermode=0", "asset", SearchViewFlags.OpenInBuilderMode | SearchViewFlags.GridView)]
public Texture pixelArtTexture;
```

To learn more about scripting components, refer to [Create components with scripts](/engine/6000.5/manual/working-with-gameobjects/unity-components/creating-components.md).

## Examples of context definitions

The following examples demonstrate the queries you can use with `SearchContext` for different workflows.

### Find all scripts where the object selection window is in list view

This query searches only the Assets (`asset`) search provider.

```cs
[SearchContext("p:t:script", "asset", SearchViewFlags.ListView)]
public MonoScript myProjectScript;
```

### Find all scripts with the word "overlay" in their name

This query searches the Asset Database (`adb`) for objects in both the `Assets` folder and the project's packages (SearchViewFlags.Packages).

```cs
[SearchContext("p:t:script overlay", "adb", SearchViewFlags.Packages | SearchViewFlags.CompactView)]
public MonoScript myPackageScript;
```

### Find all materials with a shader that contains the word "New"

Use `SearchViewFlags.HideSearchBar` to hide the search bar, which limits the user's ability to modify the context.

```cs
[SearchContext("p:shader:New", assetProviders, SearchViewFlags.HideSearchBar)]
public Material materialNoSearchBar;
```

### Find shaders or materials by shader name

Use a simple query to find the shader itself:

```cs
[SearchContext("p:t:shader unlit", "asset", SearchViewFlags.OpenInBuilderMode | SearchViewFlags.ListView)]
public Shader unlitShader;
```

Use the `ref:` filter and [Search expressions](/engine/6000.5/manual/unity-editor/editor-navigating-managing/editor-searching/search-overview/search-textual-query/search-expressions.md) to find all the materials that reference shaders with "unlit" in their name.

```cs
// Find all material referencing any shader with unlit in its name
[SearchContext("p:t:material ref:{t:shader unlit}", "asset")]
public Material materialWithUnlitShader;
```

### Define a context from a saved search query

Use a saved [Search query](/engine/6000.5/manual/unity-editor/editor-navigating-managing/editor-searching/search-overview/search-queries.md) as a context by referencing either its path or GUID:

```cs
// Open Picker with a preloaded SearchQueryAsset specified by its path
[SearchContext("Assets/Queries/t_sprite.asset", assetProviders)]
public Sprite searchQueryPathSprite;

// Open Picker with a preloaded SearchQueryAsset specified by its guid
[SearchContext("40060e4225366c64a9e24cd17cc9fdc1", assetProviders)]
public Sprite searchQueryGuidSprite;
```

### Custom Query Builder Types Selector

Use the [Visual Query Builder](/engine/6000.5/manual/unity-editor/editor-navigating-managing/editor-searching/search-overview/search-visual-query.md) language to limit the **Type** dropdown to three types: 2D texture, material, and shader.

```cs
[SearchContext("p: t:<$list:Texture2D, [Texture2D, Material, Shader]$>", "asset", SearchViewFlags.OpenInBuilderMode | SearchViewFlags.DisableBuilderModeToggle)]
public UnityEngine.Object myObjectOfConstrainedTypes;
```

## Additional resources

* [Use the Advanced Object Picker](/engine/6000.5/manual/working-with-gameobjects/unity-components/inspector-manage-components/search-advanced-object-picker.md)
* [Visual query builder reference](/engine/6000.5/manual/unity-editor/editor-navigating-managing/editor-searching/search-overview/search-visual-query.md)
* [Textual query references](/engine/6000.5/manual/unity-editor/editor-navigating-managing/editor-searching/search-overview/search-textual-query.md)
* [Create components with scripts](/engine/6000.5/manual/working-with-gameobjects/unity-components/creating-components.md)
