docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Create datasets

    You can use the Unity Cloud Assets package to view and create datasets within an asset.

    The SDK supports different workflows for users with different roles.

    Organization or Asset Manager Project role View datasets Create datasets
    Asset Management Viewer yes no
    Asset Management Consumer yes no
    Asset Management Contributor yes yes
    Organization Owner yes yes

    Before you start

    Before you start, you must:

    1. Set up a Unity scene in the Unity Editor with an Organization and Project browser. See Get started with Asset Management for more information.

    2. 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 create assets through the dashboard; see the Managing assets on the dashboard documentation.

    How do I...?

    List datasets

    To list the datasets of an asset, open the AssetManagementBehaviour script you created and add the following code to the end of the class:

    
    public List<IDataset> Datasets { get; } = new();
    public DatasetId? CurrentDatasetId { get; set; }
    public Dictionary<DatasetId, DatasetProperties> DatasetProperties { get; } = new();
    
    CancellationTokenSource m_DatasetCancellationSource;
    
    public async Task GetDatasets()
    {
        var datasetId = CurrentDatasetId;
        CurrentDatasetId = null;
        Datasets.Clear();
        DatasetProperties.Clear();
    
        var token = GetDatasetCancellationToken();
    
        await CurrentAsset.RefreshAsync(token);
    
        var asyncList = CurrentAsset.ListDatasetsAsync(Range.All, token);
        await foreach (var dataset in asyncList)
        {
            Datasets.Add(dataset);
    
            if (datasetId == dataset.Descriptor.DatasetId)
            {
                CurrentDatasetId = datasetId;
            }
    
            DatasetProperties[dataset.Descriptor.DatasetId] = await dataset.GetPropertiesAsync(token);
        }
    }
    
    public string GetDatasetName(DatasetId datasetId) => DatasetProperties.TryGetValue(datasetId, out var properties)
        ? properties.Name
        : $"{datasetId}";
    
    CancellationToken GetDatasetCancellationToken()
    {
        if (m_DatasetCancellationSource != null)
        {
            m_DatasetCancellationSource.Cancel();
            m_DatasetCancellationSource.Dispose();
        }
    
        m_DatasetCancellationSource = new CancellationTokenSource();
        return m_DatasetCancellationSource.Token;
    }
    
    

    The code snippet populates a list of datasets for the selected asset.

    Create a dataset

    To create a new dataset, open the AssetManagementBehaviour script you created and add the following code to the end of the class:

    
    public async Task CreateDataset(string name)
    {
        IDatasetCreation datasetCreation = new DatasetCreation(name)
        {
            Description = "Documentation example asset dataset creation.",
            Tags = new List<string> {"Custom"}
        };
    
        try
        {
            var dataset = await CurrentAsset.CreateDatasetAsync(datasetCreation, CancellationToken.None);
            var properties = await dataset.GetPropertiesAsync(CancellationToken.None);
            DatasetProperties[dataset.Descriptor.DatasetId] = properties;
    
            Debug.Log($"Asset dataset creation: {properties.Name} added.");
        }
        catch (Exception e)
        {
            Debug.LogError($"Failed to create dataset. {e}");
            throw;
        }
    }
    
    

    The code snippet creates a new dataset with the given name and the tag Custom on the selected asset.

    Add the UI for listing datasets

    To create UI for listing datasets, 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. Name your script UseCaseCreateDatasetExampleUI.
    4. Open the UseCaseCreateDatasetExampleUI script you created and replace the contents of the file with the following code sample:
    
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using UnityEngine;
    using Unity.Cloud.Assets;
    using Unity.Cloud.Common;
    
    public class UseCaseCreateDatasetExampleUI : IAssetManagementUI
    {
        readonly BaseAssetBehaviour m_Behaviour;
    
        public UseCaseCreateDatasetExampleUI(BaseAssetBehaviour behaviour)
        {
            m_Behaviour = behaviour;
        }
    
        public void OnGUI() { }
    }
    
    
    1. In the same script, replace the OnGUI function with the following code:
    
    string m_NewDatasetName = "MyDataset";
    
    IAsset m_CurrentAsset;
    Vector2 m_ListScrollPosition;
    
    public void OnGUI()
    {
        if (m_Behaviour.CurrentAsset == null) return;
    
        if (m_CurrentAsset != m_Behaviour.CurrentAsset)
        {
            m_CurrentAsset = m_Behaviour.CurrentAsset;
            _ = m_Behaviour.GetDatasets();
        }
    
        GUILayout.BeginVertical();
    
        GUILayout.BeginHorizontal();
    
        m_NewDatasetName = GUILayout.TextField(m_NewDatasetName, GUILayout.MinWidth(100f));
        if (GUILayout.Button("Create Dataset"))
        {
            _ = m_Behaviour.CreateDataset(m_NewDatasetName);
        }
    
        GUILayout.EndHorizontal();
    
        GUILayout.Space(15f);
    
        if (GUILayout.Button("Refresh", GUILayout.Width(60)))
        {
            _ = m_Behaviour.GetDatasets();
        }
    
        DisplayDatasets(m_Behaviour.Datasets?.ToArray() ?? Array.Empty<IDataset>());
    
        GUILayout.EndVertical();
    
        GUILayout.BeginVertical();
    
        DisplaySelectedDataset();
    
        GUILayout.EndVertical();
    }
    
    void DisplayDatasets(IReadOnlyList<IDataset> datasets)
    {
        if (datasets.Count == 0)
        {
            GUILayout.Label(" ! No datasets !");
            return;
        }
    
        m_ListScrollPosition = GUILayout.BeginScrollView(m_ListScrollPosition, GUILayout.ExpandHeight(true));
    
        foreach (var dataset in datasets)
        {
            GUILayout.BeginHorizontal();
    
            GUILayout.Label(m_Behaviour.DatasetProperties.TryGetValue(dataset.Descriptor.DatasetId, out var properties)
                ? $"{properties.Name}"
                : $"{dataset.Descriptor.DatasetId}");
    
            GUI.enabled = m_Behaviour.CurrentDatasetId != dataset.Descriptor.DatasetId;
    
            if (GUILayout.Button("Select", GUILayout.Width(60)))
            {
                m_Behaviour.CurrentDatasetId = dataset.Descriptor.DatasetId;
            }
    
            GUI.enabled = true;
    
            GUILayout.EndHorizontal();
        }
    
        GUILayout.EndScrollView();
    }
    
    void DisplaySelectedDataset()
    {
        if (m_Behaviour.CurrentDatasetId == null) return;
    
        if (!m_Behaviour.DatasetProperties.TryGetValue(m_Behaviour.CurrentDatasetId.Value, out var properties))
        {
            GUILayout.Label("Loading properties...");
            return;
        }
    
        GUILayout.Label($"Description: {properties.Description}");
        GUILayout.Label($"Type: {properties.Type}");
        GUILayout.Label($"Tags: {string.Join(", ", properties.Tags)}");
        GUILayout.Label($"System Tags: {string.Join(", ", properties.SystemTags)}");
        GUILayout.Label($"Status: {properties.StatusName}");
        GUILayout.Label("Is Visible: " + (properties.IsVisible ? "Yes" : "No"));
    }
    
    
    1. Open the AssetManagementUI script you created 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 AssetSelectionExampleUI(m_Behaviour));
    m_UI.Add(new UseCaseCreateDatasetExampleUI(m_Behaviour));
    
    

    The code snippet does the following:

    • Displays a list of datasets for the selected asset.
    • Provides a text input and UI button to create a new dataset.
    • Logs a confirmation message to the console when a dataset is created.
    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)