Use case: Group and count assets
You can use the Unity Cloud Assets 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.
Organization or Asset Manager Project role | Group and count search |
---|---|
Asset Management Viewer |
yes |
Asset Management Consumer |
yes |
Asset Management Contributor |
yes |
Organization Owner |
yes |
Before you start
Before you start, you must:
Set up a Unity scene in the Unity Editor with an Organization and Project browser. See Get started with Assets 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 Assets.
- 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 AssetManagementBehaviour
script you created and add the following code to the end of the class:
public IReadOnlyDictionary<string, int> GroupCounters { get; private set; }
public int Total { get; private set; }
public async Task AggregateByField(GroupableField groupableField)
{
GroupCounters = null;
GroupCounters = await CurrentProject.GroupAndCountAssets().ExecuteAsync(groupableField, CancellationToken.None);
Total = GroupCounters.Values.Sum();
}
public async Task AggregateByCollection()
{
GroupCounters = null;
var collections = await CurrentProject.GroupAndCountAssets().GroupByCollectionAndExecuteAsync(CancellationToken.None);
GroupCounters = collections.ToDictionary(x => x.Key.Path.ToString(), x => x.Value);
Total = GroupCounters.Values.Sum();
}
The code snippet provides a functions which returns the aggregation of assets for a given field.
Add the UI for displaying aggregation information
To create UI for displaying aggregation information, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scripts
folder. - Go to Create > C# Script. Name your script
UseCaseAggregationExampleUI
. - Open the
UseCaseAggregationExampleUI
script you created and replace the contents of the file with the following code sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using UnityEngine;
public class UseCaseAggregationExampleUI : IAssetManagementUI
{
readonly AssetManagementBehaviour m_Behaviour;
public UseCaseAggregationExampleUI(AssetManagementBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUI
function with the following code:
readonly string[] m_AggregationFields = Enum.GetNames(typeof(GroupableField));
int m_SelectedIndex = -1;
public void OnGUI()
{
if (!m_Behaviour.IsProjectSelected)
{
m_SelectedIndex = -1;
return;
}
GUILayout.Space(15f);
GUILayout.BeginVertical();
GUILayout.Label("Aggregate by: ");
var selection = GUILayout.SelectionGrid(m_SelectedIndex, m_AggregationFields, 4);
if (selection != m_SelectedIndex && selection >= 0)
{
m_SelectedIndex = selection;
var aggregationField = (GroupableField) Enum.Parse(typeof(GroupableField), m_AggregationFields[m_SelectedIndex]);
_ = m_Behaviour.AggregateByField(aggregationField);
}
if (GUILayout.Button("Collections"))
{
_ = m_Behaviour.AggregateByCollection();
}
GUILayout.Label("Aggregation Results:");
if (m_Behaviour.GroupCounters != null)
{
GUILayout.Label($"Total: {m_Behaviour.Total}");
GUILayout.Label($"Unique: {m_Behaviour.GroupCounters.Keys.Count()}");
GUILayout.Label($"Values:");
foreach (var value in m_Behaviour.GroupCounters)
{
GUILayout.Label($"- {value.Key}: {value.Value}");
}
}
else
{
GUILayout.Label("Empty.");
}
GUILayout.EndVertical();
}
- Open the
AssetManagementUI
script you created and replace the contents of theAwake
function with the following code:
m_UI.Add(new OrganizationSelectionExampleUI(m_Behaviour));
m_UI.Add(new ProjectSelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseAggregationExampleUI(m_Behaviour));
The code snippet provides UI buttons to trigger the aggregation function with different criteria and displays the results of the aggregation. The UI can aggregate assets by:
- Name
- Version
- Type
- Status
- Tags and system tags
- Preview file
- Created by
- Updated by
- Collections
The results of the aggregation are displayed below the buttons.
Total
is the number of assets matching the search criteria.Unique
is the number of unique values for the aggregation field.Values
is a list of key-value pairs, where the key is a unique value of the aggregation field and the value is the number of assets that match.