docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Group and count assets

    Use the Unity Cloud Assets package to count 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 Manager Viewer yes
    Asset Manager Consumer yes
    Asset Manager Contributor yes
    Asset Manager Admin yes

    Before you start

    Before you start, do the following:

    1. Verify you have the required permissions. Read more about verifying permissions.

      Note

      Asset Manager roles define the permissions that you have for a single Asset Manager project. Depending on your work, permissions may vary across projects.

    2. Set up a Unity scene in the Unity Editor with an Organization and Project browser. Read more about setting up a Unity scene.

    3. Create assets in Unity Cloud any of the following ways:

      • Add assets using the Asset SDK.
      • Add a single asset or multiple assets through the dashboard.

    How do I...?

    Add aggregation behaviors

    To implement aggregation, follow these steps:

    1. Open the AssetManagementBehaviour script that you created as described in Get started with Asset SDK.
    2. Add the following code to the end of the class:
    
    readonly Dictionary<string, int> m_GroupCounters = new();
    
    public IReadOnlyDictionary<string, int> GroupCounters => m_GroupCounters;
    public int Total { get; private set; }
    
    public async Task AggregateByField(Groupable groupable)
    {
        m_GroupCounters.Clear();
        Total = -1;
    
        var asyncEnumerable = CurrentProject.GroupAndCountAssets().ExecuteAsync(groupable, CancellationToken.None);
        await foreach (var group in asyncEnumerable)
        {
            switch (group.Key.Type)
            {
                case GroupableFieldValueType.CollectionDescriptor:
                    m_GroupCounters.Add("[Collection] " + group.Key.AsCollectionDescriptor().Path, group.Value);
                    break;
                default:
                    m_GroupCounters.Add($"[{group.Key.Type}] " + group.Key.AsString(), group.Value);
                    break;
            }
    
            if (Total == -1)
            {
                Total = 0;
            }
    
            Total += group.Value;
        }
    
        if (Total == -1)
        {
            Total = 0;
        }
    }
    
    

    The code snippet provides a function that aggregates assets based on a specific field.

    Add a UI for displaying aggregation information

    To create a UI for displaying aggregation information, follow these steps:

    1. In your Unity Project window, go to Assets > Scripts.
    2. Select and hold the Assets/Scripts folder.
    3. Go to Create > C# Script.
    4. Name your script UseCaseAggregationExampleUI.
    5. Open the UseCaseAggregationExampleUI script that you created in the previous step 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() { }
    }
    
    
    1. In the same script, replace the OnGUI function with the following code:
    
    readonly string[] m_AggregationFields = Enum.GetNames(typeof(GroupableField));
    int m_SelectedIndex = -1;
    
    Vector2 m_ScrollPosition;
    
    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);
        }
    
        GUILayout.Label("Aggregation Results:");
        if (m_Behaviour.Total > 0)
        {
            GUILayout.Label($"Total: {m_Behaviour.Total}");
            GUILayout.Label($"Unique: {m_Behaviour.GroupCounters.Keys.Count()}");
            GUILayout.Label($"Values:");
            
            m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition, GUILayout.ExpandHeight(true));
    
            foreach (var value in m_Behaviour.GroupCounters)
            {
                GUILayout.Label($"- {value.Key}: {value.Value}");
            }
    
            GUILayout.EndScrollView();
        }
        else if (m_Behaviour.Total == 0)
        {
            GUILayout.Label("No results.");
        }
    
        GUILayout.EndVertical();
    }
    
    
    1. Open the AssetManagementUI script that you created as described in Get started with Asset SDK and replace the contents of the Awake 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 does the following:

    • Provides UI buttons that trigger the aggregation function with different criteria.
    • Displays the results of the aggregation.

    The UI can aggregate assets by the following criteria:

    • Name
    • Version
    • Type
    • Status
    • Tags and system tags
    • Preview file
    • Created by
    • Updated by
    • Collections

    Results of the aggregation are displayed below the following buttons:

    • Total is the number of assets that match 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 the search criteria.
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)