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; set; }
public IDataset CurrentDataset { get; private set; }
public async Task GetDatasets()
{
Datasets = new List<IDataset>();
CurrentDataset = null;
_ = CurrentAsset.RefreshAsync(CancellationToken.None);
var asyncList = CurrentAsset.ListDatasetsAsync(Range.All, CancellationToken.None);
await foreach (var dataset in asyncList)
{
Datasets.Add(dataset);
}
}
public void SetCurrentDataset(IDataset dataset)
{
CurrentDataset = dataset;
}
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)
{
try
{
await CurrentDataset.UpdateAsync(update, CancellationToken.None);
await CurrentDataset.RefreshAsync(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/Scripts
folder. - Go to Create > C# Script. Name your script
UseCaseDatasetCreationExampleUI
. - Open the
UseCaseDatasetCreationExampleUI
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 UseCaseDatasetUpdateExampleUI : IAssetManagementUI
{
readonly AssetManagementBehaviour m_Behaviour;
public UseCaseDatasetUpdateExampleUI(AssetManagementBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUI
function with the following code:
IAsset m_CurrentAsset;
Vector2 m_DatasetListScrollPosition;
DatasetUpdate m_DatasetUpdate;
public void OnGUI()
{
if (!m_Behaviour.IsProjectSelected) return;
if (m_CurrentAsset != m_Behaviour.CurrentAsset)
{
m_CurrentAsset = m_Behaviour.CurrentAsset;
m_Behaviour.Datasets = null;
}
if (m_CurrentAsset == null)
{
GUILayout.Label(" ! No asset selected !");
return;
}
GUILayout.BeginVertical();
if (GUILayout.Button("Refresh", GUILayout.Width(60)) || m_Behaviour.Datasets == null)
{
_ = m_Behaviour.GetDatasets();
}
GUILayout.Label("Asset datasets:");
DisplayDatasets(m_Behaviour.Datasets?.ToArray() ?? Array.Empty<IDataset>());
GUILayout.EndVertical();
if (m_Behaviour.CurrentDataset == null)
{
GUILayout.Label(" ! No dataset selected !");
return;
}
GUILayout.BeginVertical();
DisplayDataset();
GUILayout.EndVertical();
}
void DisplayDatasets(IReadOnlyList<IDataset> datasets)
{
if (datasets.Count == 0)
{
GUILayout.Label(" ! No datasets !");
}
else
{
m_DatasetListScrollPosition = GUILayout.BeginScrollView(m_DatasetListScrollPosition, GUILayout.Height(Screen.height * 0.8f));
for (var i = 0; i < datasets.Count; ++i)
{
GUILayout.BeginHorizontal();
var dataset = datasets[i];
GUILayout.Label($"{dataset.Name}");
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.SetCurrentDataset(dataset);
m_DatasetUpdate = new DatasetUpdate(dataset);
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
}
void DisplayDataset()
{
GUILayout.Label("Name:");
m_DatasetUpdate.Name = GUILayout.TextField(m_DatasetUpdate.Name);
GUILayout.Label("Description:");
m_DatasetUpdate.Description = GUILayout.TextArea(m_DatasetUpdate.Description);
m_DatasetUpdate.IsVisible = GUILayout.Toggle(m_DatasetUpdate.IsVisible ?? false, "Is visible");
GUILayout.Label("Tags:");
var tags = GUILayout.TextField(string.Join(",", m_DatasetUpdate.Tags));
m_DatasetUpdate.Tags = tags.Split(',').Select(tag => tag.Trim()).ToList();
if (GUILayout.Button("Update"))
{
_ = UpdateDataset();
}
}
async Task UpdateDataset()
{
await m_Behaviour.UpdateDataset(m_DatasetUpdate);
m_DatasetUpdate = new DatasetUpdate(m_Behaviour.CurrentDataset);
}
- 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 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.