Search expressions allow you to add to the search query language to express complex queries that cross-reference multiple providers, for example, to search for all objects in a sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary that use a shaderA program that runs on the GPU. More info
See in Glossary that doesn’t compile.
Search expressions can be chained together to transform or perform set manipulations on Search Items.
Enter a search expression in the Search window to return a result:
A search expression starts with a root expression that can contain inner expressions.
A simple query like t:shader
is an expression. It returns all of the Search Items corresponding to shaders in your project.
Use the search expression language to combine multiple queries into a single expression to create more flexible searches.
Search expressions support the following:
t:shader
t:prefab ref={t:texture}
count{t:shader}
3
or "this is a string literal"
@path or @mesh
(any identifier beginning with @
is assumed to be a selector that extracts a value from a Search Item. Selectors can find properties of an Object or be used to compute results dynamically)All search expressions return a set of Search Items (IEnumerable<SearchItem>
). As a general guideline, the search expression language uses curly braces {}
to denote a set of items. Even literal expressions such as 3
are evaluated internally as a set : {3}
.
Search expressions can be chained together to make more complex queries:
t:prefab ref={t:texture}
t:texture
part of the query finds all textures in your projectt:prefab ref=
part of the query runs a query for each texture returned in the t:texture set to see which prefabsAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
t:prefab ref={t:texture size>4000}
: finds all prefabs in a project with texture size larger than 4000 bytes.
This is the equivalent to:
Running t:texture size>4000
to return a list of all 4K textures in the project (for example, armor.png
, treasure.png
).
Running t:prefab ref=<one of the 4K textures>
(for example, t:prefab ref=armor.png
and then t:prefab ref=treasure.png
).
Aggregating all results and returning this as a single list of Search Items.
t:[shader, material, texture]
: finds all objects whose types are shader
, material
, or texture
. Using a normal query it could be expressed as:
t:shader or t:material or t:texture
Unity has a library of search expression functions to manipulate and transform sets of Search Items. Each of these functions can take multiple arguments and returns a set of items. Unity Search Expressions use curly braces to denote function calls.
The complete list of all functions is here.
Example: The function count counts the items of each set passed as parameters and returns a number.
count{t:shader}
: returns a set containing the number of shaders in the project. Example: {34}
.count{t:shader, t:texture}
: returns a set containing the number of shaders and textures in the project. Example: {34, 2048}
.Functions can be chained together, similar to s-expression used in the LISP programming language.
Note: There can only be a single root expression.
Example of a chain of functions being evaluated, in order of operation:
print{"path=@path", first{10, sort{t:texture, @size}}}
: print the path of the 10 largest textures in the project
t:texture
: finds all texture in the project and then selects the size property.sort{ t:texture, @size}
: sorts all these textures according to their size property.first{10
: selects the first 10 sorted textures.print{ "path=@path"
: prints this list in the console according to a format string where Unity Search extracts the path property of each resulting item path=@path
.Functions can have multiple signatures (name of the method and the type and kind (value, reference, or output) of each of its formal parameters, similar to C#) and can support optional (parameters that don’t need to be passed to the function) and variadic (parameters that can take a variable number of arguments) parameters.
Literals are actual words or numbers that you would like to search for or number amounts you want to retrieve, as opposed to the query string. For example, t:texture searches for Assets with texture in the type name (e.g.,Texture2D), but adding quotation marks to make it a literal, “t:texture” searches for an Asset named t:texture.
Expression | Description |
---|---|
Number | A literal number (1,2,3, and so on ) |
Set | Square brackets ([ ] ) |
String | Single or double quotes ('' or "" ) |
Number literals can used as parameters to functions (such as first
).
first{10, t:shader}
-> {the first 10 shaders return by the 't:shader' query}
Express a set of values using square brackets []
. A set can contain any type of expression, but the search expression parser will assume the elements of a set are literals instead of a search query.
Example:
[t:shader, hello, 3]
-> ["t:shader", "hello", 3]
If using curly brackets ({}
), the parser would treat this as three queries:
{t:shader, hello, 3}
-> {<run a query for t:shader>, <run a query for hello>, <run a query for 3>}
String literals can be used as parameters for some functions (such as format
). You can specify a string literal using single or double quotes:
“hello” or ‘hello’
The format function taking a format string as a parameter:
format{t:texture, '@path (@size)'}
A selector is an identifier denoted using the @
prefix. Use selectors to access the property of an item in order to compute, filter, or format. Selectors can map to serialized properties of a UnityEngine.Object or to a custom function to allow access to dynamic data.
The base selectors that any Search Items supports are:
id
: Unique ID of this item according to its Search Provider.value
: Internal value of an item. By default it is its ID, but functions can override the value of an item.label
: item label as shown in the Search windowdesc
or description
: item description (second line of the Search window)Unity also defines generic selectors for a Search Item, for example:
@name
: UnityEngine.Object.name@size
: File size on disk for an asset@path
: Asset path@extension
: Asset file extension@provider
: Search Provider that has yielded this itemTo access specific properties of a Search Item in order to perform an operation, use selectors:
count{t={distinct{select{a:assets, @type}}}}
a:assets, @type
finds all of the assets in a project and selects the type
property for those objects.distinct
returns a list of all types that exist.t={list of types}
returns the multiple lists of each asset of each type.count
counts how many assets of each type are in your project.avg{@size,t:shader}
t:shader, @size
: finds all the shaders and then selects the size property.avg
: computes the average size for all shaders in your project.print{@value, count{t:texture, t:material}}
The @#
selector finds serialized properties and material properties:
sort{t:texture, @#height}
: sorts all textures in order of their height serialized properties.You can name search expressions to make them easier to display in the Search window.
For example, entering the expression sort{count{t:audio, t:texture}, @value,desc}
in the Search window, it may be difficult to read which count corresponds to which type:
Using an alias: sort{count{t:audio as Audio, t:texture as Texture}, desc}
yields a more readable result:
To dynamically generate alias names, use the alias
function. For example:
alias{[1, 2, 3], 'Title value'}
will yield items with these labels:
{Title 1, Title 2, Title 3}
The expand operator ...
allows a set of items to be grouped into multiple sets of items instead of just one set.
...{expandable expression}
-> {sub expr 1} {sub expr 2} {sub expr N}
Example:
A small project has 3 prefabs, 4 textures and 5 shaders. The following expression provides a count for all:
count{t:[prefab, texture, shader]}
-> {12}
The result of the search expression t:[prefab, texture, shader]
is a unified list of 12 items of type prefabs, textures, and shaders. This could also be expressed as: t:prefab or t:texture or t:shader
.
Use the expand operator to count the number of each type of asset separately:
count{...t:[prefab, texture, shader]}
-> count{t:prefab, t:texture, t:shader}
-> {3, 4, 5}
The groupBy
function can be used with the expand operator to return sets of items grouped by a common key.
Example:
A project has three types of assets (prefabs, textures, and shaders). For a selector @type
that returns the type of Search Item, the expression would expand to:
count{t:prefab or t:texture or t:shader}
You can use the groupBy
function to group items by their type and then count each number of items per type:
count{...groupBy{t:prefab or t:texture or t:shader, @type}}
These examples demonstrate how search expressions can be used for complex requests in a project.
Count prefab usage in scenes and sort by highest usage:
sort{select{p: t:prefab *.prefab, @path, count{t:scene ref:@path} as count}, @count, desc}
Sort and count all asset types
sort{count{...groupby{a:assets, @type}}, @value, desc}
Find the meshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
See in Glossary with the most vertices (assuming the @vertices selector is available in the project).
first{sort{t:mesh, @vertices, desc}}
Sort all meshes by their vertex count
sort{select{h: t:mesh, @path, @vertices}, @vertices, desc}
Count the number of vertices for all meshes
sum{select{h:t:mesh, @vertices}}
Find all assets referencing a mesh
ref=select{p:t:mesh, @path}
List prefab scene references count
select{p: *.prefab, @path, count{p: a:sceneIndex ref="@path"} as count}
Search expression evaluation is based on the C# iterator pattern and uses of the yield keyword. This ensures that the evaluation is exposed as a fully asynchronous operation and is as non-blocking as possible. It allows each search expression to start its computation before the initial sets of search items are fully computed.
By default all search expression evaluation is done in a worker thread in order to not block the Editor. For operations that need to rely on non-thread safe Unity API (like some selector accessors), we have a utility API to queue those operations on the main thread and maintain the pattern of yielding search items.
The search expression language has been designed to be customizable. APIs to customize all parts of the framework will be available in a future release.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.