Aggregate assets
You can use the Unity Asset Manager SDK package to retrieve the number of assets in a project that meet a set of search criteria.
The SDK supports different workflows for users with different roles.
| Asset Manager Project role | Supported workflows | Details |
|---|---|---|
Asset Management Viewer (minimum) |
Asset Discovery | The CloudAssetDiscovery implementation of the IAssetProvider interface fetches and searches among published assets only. |
Asset Management Contributor (minimum) |
Asset Management | The CloudAssetManager implementation of the IAssetProvider interface fetches and searches among all assets regardless of status. |
Before you start
Before you start, you must:
- Set up a Unity scene in the Unity Editor with an organization and project browser. See either Get started with Asset Discovery or Get started with Asset Management for more information.
Have some assets in the cloud. There are several ways to do so:
- You can create assets through the Get started with Asset Management.
- You can upload assets from existing Unity assets; see the Asset Database Uploader sample.
- You can create assets through the dashboard; see the Managing assets on the dashboard documentation.
How do I...?
Add aggregation behaviours
To implement aggregation, open the AssetDiscoveryBehaviour script you created and add the following code to the end of the class:
public Aggregation Aggregation { get; private set; }
public async Task AggregateByField(string aggregationField)
{
var assetSearchFilter = new AssetSearchFilter(m_CurrentOrganization, m_CurrentProject);
var aggregationParameters = new AggregationParameters(aggregationField);
var cancellationTokenSrc = new CancellationTokenSource();
Aggregation = await PlatformServices.AssetProvider.AggregateAsync(assetSearchFilter, aggregationParameters, cancellationTokenSrc.Token);
}
Note: You can adapt the code to the Asset Management pathway. To do so, add the above code to the
AssetManagementBehaviourscript you created and replace references toPlatformServices.AssetProvidertoPlatformServices.AssetManager.
The script provides several functions which return the aggregation of assets for a given field.
Add the UI for triggering and displaying the aggregation
To add UI for the example, open the AssetDiscoveryUI script you created and replace the AssetActions function with the following code:
protected virtual void AssetActions()
{
if (GUILayout.Button("Aggregate by Type"))
{
_ = m_Behaviour.AggregateByField(nameof(IAsset.Type));
}
if (GUILayout.Button("Aggregate by Tag"))
{
_ = m_Behaviour.AggregateByField(nameof(IAsset.Tags));
}
GUILayout.Label("Aggregation Results:");
if (m_Behaviour.Aggregation != null)
{
GUILayout.Label($"Total: {m_Behaviour.Aggregation.Total}");
GUILayout.Label($"Unique: {m_Behaviour.Aggregation.Unique}");
GUILayout.Label($"Values:");
foreach (var value in m_Behaviour.Aggregation.Values)
{
GUILayout.Label($"- {value.Key}: {value.Value}");
}
}
else
{
GUILayout.Label("Empty.");
}
}
Note: You can adapt the code to the Asset Management pathway. To do so, add the above code to the
AssetManagementUIscript you created.
The script provides UI buttons to trigger the aggregation functions and displays the results of the aggregation functions.