Use case: Search assets in a project
You can use the Unity Cloud Assets package to filter assets in a Project based on a set of search criteria.
Organization or Asset Manager Project role | Search |
---|---|
Asset Management Viewer |
yes |
Asset Management Consumer |
yes |
Asset Management Contributor |
yes |
Organization Owner |
yes |
How do I...?
Built-in Search Filter
The Unity Assets package provides a built-in search filter that you can use to search for assets: the AssetSearchFilter
class.
The AssetSearchFilter
class provides the set of properties that can be used to filter assets.
Create a new search filter
You can create a new search filter by instantiating the AssetSearchFilter
class, like so:
var assetSearchFilter = new AssetSearchFilter();
Each searchable property provides 3 avenues for searching:
Include
- The property must match the value exactly.Exclude
- The property must not match the value.Any
- The property may contain the value. This represents anOR
operation to be applied on all properties that include theAny
value.
To compute the search results, you can use the SearchAssetsAsync
method of an IAssetProject
, like so:
return project.QueryAssets().SelectWhereMatchesFilter(assetSearchFilter).LimitTo(new Range(0, 10)).ExecuteAsync(CancellationToken.None);
The Pagination
struct is used to control the range of results to be returned and the ordering of results.
In this example, the first 10 results displayed are sorted by the asset name in ascending order.
The Search
method returns an awaitable IAsyncEnumerable
that will return each IAsset
result.
The results can be iterated over using a foreach
loop and used as they become available, like so:
var assets = project.QueryAssets().SelectWhereMatchesFilter(assetSearchFilter).ExecuteAsync(CancellationToken.None);
await foreach (var asset in assets)
{
Debug.Log(asset.Name + " is available for use.");
// Do something with each `asset` as it becomes available.
}
Alternatively, the results can be iterated over and compiled into a list, so that the complete set of results can be used, like so:
var assets = project.QueryAssets().SelectWhereMatchesFilter(assetSearchFilter).ExecuteAsync(CancellationToken.None);
var assetList = new List<IAsset>();
await foreach (var asset in assets)
{
assetList.Add(asset);
}
// Do something with the complete `assetList`
Search by Name
- You can search for assets by name using the
Name
property of theAssetSearchFilter
class, like so:
assetSearchFilter.Include().Name.WithValue("my cool asset");
Note
This type of search checks for assets whose entire name exactly matches the parameter.
- You can also exclude assets by name, like so:
assetSearchFilter.Exclude().Name.WithValue("my mediocre asset");
- You can also search for assets whose name contains a specific string, like so:
assetSearchFilter.Any().Name.WithValue("cool");
Search by Tags
You can search for assets by tag using the Tags
property of the AssetSearchFilter
class, like so:
assetSearchFilter.Include().Tags.WithValue("tag1", "tag2", "tag3");
Note
This type of search checks for assets whose tag list contains all the included parameters.
Filter by Collections
You can search for assets in specific collections by adding them to the search filter's list of collections, like so:
assetSearchFilter.Collections.WhereContains("my awesome collection", "my other awesome collection");
Custom Search Filter
You can also create a custom search filter by implementing the IAssetSearchFilter
interface.