Use case: Search assets across projects
You can use the Unity Cloud Assets package to filter assets across projects based on a set of search criteria.
Organization or Asset Manager Project role | Cross-project search |
---|---|
Asset Management Viewer |
yes |
Asset Management Consumer |
yes |
Asset Management Contributor |
yes |
Organization Owner |
yes |
Methodology
Built-in Search Filter
The Unity Cloud 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.
Search filter properties
Searches are done across specified projects.
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:
IAsyncEnumerable<IAsset> SearchAsync(IEnumerable<ProjectDescriptor> projectDescriptors, IAssetSearchFilter assetSearchFilter)
{
return m_AssetRepository.QueryAssets(projectDescriptors).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, we display the first 10 results sorted by the asset name in ascending order.
The SearchAsync
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:
async Task DisplayResultsIndividually(IEnumerable<ProjectDescriptor> projectDescriptors, IAssetSearchFilter assetSearchFilter)
{
var assets = m_AssetRepository.QueryAssets(projectDescriptors).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:
async Task<IEnumerable<IAsset>> DisplayResults(IEnumerable<ProjectDescriptor> projectDescriptors, IAssetSearchFilter assetSearchFilter)
{
var assets = m_AssetRepository.QueryAssets(projectDescriptors).SelectWhereMatchesFilter(assetSearchFilter).ExecuteAsync(CancellationToken.None);
var assetList = new List<IAsset>();
await foreach (var asset in assets)
{
assetList.Add(asset);
}
return assetList;
}
Search by Name
You can search for assets by name using the Name
property of the AssetSearchFilter
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
To create a custom search filter, you can implement the IAssetSearchFilter
interface.