Use case: Update datasets
You can use the Unity Cloud Assets package to view and update existing datasets within an asset.
The SDK supports different workflows for users with different roles.
| Organization or Asset Manager Project role | View datasets | Update 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:
Set up a Unity scene in the Unity Editor with an Organization and Project browser. See 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 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();
public async Task GetDatasets()
{
var datasetId = CurrentDatasetId;
CurrentDatasetId = null;
Datasets.Clear();
DatasetProperties.Clear();
await CurrentAsset.RefreshAsync(CancellationToken.None);
var asyncList = CurrentAsset.ListDatasetsAsync(Range.All, CancellationToken.None);
await foreach (var dataset in asyncList)
{
Datasets.Add(dataset);
if (datasetId == dataset.Descriptor.DatasetId)
{
CurrentDatasetId = datasetId;
}
DatasetProperties[dataset.Descriptor.DatasetId] = await dataset.GetPropertiesAsync(CancellationToken.None);
}
}
The code snippet populates a list of datasets for the selected asset.
Update a dataset
To update an existing dataset, open the AssetManagementBehaviour script you created and add the following code to the end of the class:
public async Task UpdateDataset(IDatasetUpdate update)
{
if (CurrentDatasetId == null) return;
try
{
var datasetId = CurrentDatasetId.Value;
var dataset = Datasets.FirstOrDefault(x => x.Descriptor.DatasetId == CurrentDatasetId)
?? await CurrentAsset.GetDatasetAsync(datasetId, CancellationToken.None);
await dataset.UpdateAsync(update, CancellationToken.None);
await dataset.RefreshAsync(CancellationToken.None);
DatasetProperties[datasetId] = await dataset.GetPropertiesAsync(CancellationToken.None);
Debug.Log($"Dataset update succeeded.");
}
catch (Exception e)
{
Debug.LogError($"Failed to update dataset. {e}");
throw;
}
}
The code snippet updates an existing dataset and refreshes the display.
Add the UI for listing datasets
To create UI for listing datasets, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
UseCaseDatasetCreationExampleUI. - Open the
UseCaseDatasetCreationExampleUIscript 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 Unity.Cloud.Common;
using UnityEngine;
public class UseCaseDatasetUpdateExampleUI : IAssetManagementUI
{
readonly AssetManagementBehaviour m_Behaviour;
readonly string[] m_AssetTypeList;
public UseCaseDatasetUpdateExampleUI(AssetManagementBehaviour behaviour)
{
m_Behaviour = behaviour;
m_AssetTypeList = AssetTypeExtensions.AssetTypeList().ToArray();
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
static GUILayoutOption s_LabelWidth = GUILayout.Width(60);
IAsset m_CurrentAsset;
Vector2 m_DatasetListScrollPosition;
DatasetUpdate m_DatasetUpdate;
string m_TagsString = string.Empty;
public void OnGUI()
{
if (!m_Behaviour.IsProjectSelected) return;
if (m_Behaviour.CurrentAsset == null)
{
GUILayout.Label(" ! No asset selected !");
return;
}
if (m_CurrentAsset != m_Behaviour.CurrentAsset)
{
m_CurrentAsset = m_Behaviour.CurrentAsset;
_ = m_Behaviour.GetDatasets();
}
GUILayout.BeginVertical();
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.GetDatasets();
}
GUILayout.Label("Datasets:");
DisplayDatasets(m_Behaviour.Datasets.ToArray());
GUILayout.EndVertical();
if (m_Behaviour.CurrentDatasetId == null)
{
GUILayout.Label(" ! No dataset selected !");
return;
}
GUILayout.BeginVertical();
DisplayDataset(m_Behaviour.DatasetProperties.GetValueOrDefault(m_Behaviour.CurrentDatasetId.Value));
GUILayout.EndVertical();
}
void DisplayDatasets(IReadOnlyCollection<IDataset> datasets)
{
if (datasets.Count == 0)
{
GUILayout.Label(" ! No datasets !");
return;
}
m_DatasetListScrollPosition = GUILayout.BeginScrollView(m_DatasetListScrollPosition, GUILayout.Height(Screen.height * 0.8f));
foreach (var dataset in datasets)
{
if (!m_Behaviour.DatasetProperties.TryGetValue(dataset.Descriptor.DatasetId, out var properties))
{
GUILayout.Label(dataset.Descriptor.DatasetId.ToString());
continue;
}
GUILayout.BeginHorizontal();
GUILayout.Label($"{properties.Name}");
GUI.enabled = dataset.Descriptor.DatasetId != m_Behaviour.CurrentDatasetId;
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.CurrentDatasetId = dataset.Descriptor.DatasetId;
m_DatasetUpdate = new DatasetUpdate
{
Name = properties.Name,
Type = properties.Type,
Description = properties.Description,
IsVisible = properties.IsVisible,
Tags = properties.Tags?.ToList() ?? new List<string>()
};
m_TagsString = string.Join(',', m_DatasetUpdate.Tags);
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
void DisplayDataset(DatasetProperties datasetProperties)
{
GUILayout.Label("Name:", s_LabelWidth);
m_DatasetUpdate.Name = GUILayout.TextField(m_DatasetUpdate.Name);
GUILayout.Label("Type:", s_LabelWidth);
var typeIndex = m_DatasetUpdate.Type.HasValue ? (int) m_DatasetUpdate.Type.Value : (int) datasetProperties.Type;
typeIndex = GUILayout.SelectionGrid(typeIndex, m_AssetTypeList, 4);
if (typeIndex != -1 && datasetProperties.Type != (AssetType) typeIndex)
{
m_DatasetUpdate.Type = (AssetType) typeIndex;
}
GUILayout.Label("Description:", s_LabelWidth);
m_DatasetUpdate.Description = GUILayout.TextArea(m_DatasetUpdate.Description);
m_DatasetUpdate.IsVisible = GUILayout.Toggle(m_DatasetUpdate.IsVisible ?? false, "Is visible");
GUILayout.Label("Tags:", s_LabelWidth);
m_TagsString = GUILayout.TextField(m_TagsString, GUILayout.ExpandWidth(true));
m_DatasetUpdate.Tags = m_TagsString.Split(',')
.Select(tag => tag.Trim())
.Where(tag => !string.IsNullOrEmpty(tag))
.ToList();
if (GUILayout.Button("Update"))
{
_ = m_Behaviour.UpdateDataset(m_DatasetUpdate);
}
GUILayout.Label($"System tags: {string.Join(", ", datasetProperties.SystemTags)}");
GUILayout.Label($"Workflow: {string.Join(", ", datasetProperties.WorkflowName)}");
}
- Open the
AssetManagementUIscript you created and replace the contents of theAwakefunction 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 UseCaseDatasetUpdateExampleUI(m_Behaviour));
The code snippet does the following:
- Displays a list of datasets for the selected asset with a UI button to select the dataset for update.
- When a dataset is selected, the UI displays UI input fields and a UI button to update the properties of the dataset.
- When the dataset is updated, a message is logged to the console to confirm the update.