Use case: Update an asset's properties
You can use the Unity Cloud Assets package to update the status of assets in the Cloud.
Note
To update assets, you need an Asset Manager Admin
role at Organization level or an Asset Management Contributor
add-on role at Project level. Asset Management Contributors can update assets only for the specific projects to which they have access.
Before you start
Before you start, do the following:
- Verify your permissions as described in Asset Manager user roles.
Note
Asset Manager roles specify permissions you have for a single Asset Manager project. Depending on your work, permissions can vary across different projects.
Set up a Unity scene in the Unity Editor with an Organization and Project browser. See the Get started with Asset SDK page for more information.
Create assets in the Cloud any of the following ways:
How do I...?
Update an asset
Update an asset as follows:
- Open the
AssetManagementBehaviour
script you created as described in Get started with Asset SDK. - Add the following code to the end of the class:
public async Task UpdateAssetAsync(IAssetUpdate assetUpdate)
{
try
{
await CurrentAsset.UpdateAsync(assetUpdate, CancellationToken.None);
// Update properties:
await CurrentAsset.RefreshAsync(CancellationToken.None);
var properties = await CurrentAsset.GetPropertiesAsync(CancellationToken.None);
m_Behaviour.IncludeProperties(CurrentAsset.Descriptor, properties);
}
catch (OperationCanceledException oe)
{
Debug.Log(oe);
}
catch (AggregateException e)
{
Debug.LogError(e.InnerException);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
The code snippet provides a method to update the selected asset.
Clear the properties of an asset
Properties of IAssetUpdate
set to null are not updated. Therefore, set the fields you want to clear explicitly empty.
Clear the description
Clear the description of an asset as follows:
var assetUpdate = new AssetUpdate
{
Description = ""
};
await CurrentAsset.UpdateAsync(assetUpdate, CancellationToken.None);
Clear the tags
Clear the tags of an asset as follows:
var assetUpdate = new AssetUpdate
{
Tags = new List<string>()
};
await CurrentAsset.UpdateAsync(assetUpdate, CancellationToken.None);
Clear the default preview
Clear the default preview of an asset as follows:
var assetUpdate = new AssetUpdate
{
PreviewFile = ""
};
await CurrentAsset.UpdateAsync(assetUpdate, CancellationToken.None);
Note
You cannot clear the name of an asset. The name must always be a non-empty value.
Add a UI to view and update asset properties
Create a UI to update an asset as follows:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scripts
folder. - Go to Create > C# Script.
- Name your script
UseCaseManageAssetExampleUI
. - Open the
UseCaseManageAssetExampleUI
script 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 UnityEngine;
using Unity.Cloud.Assets;
using Unity.Cloud.Common;
public class UseCaseManageAssetExampleUI : IAssetManagementUI
{
readonly BaseAssetBehaviour m_Behaviour;
readonly string[] m_AssetTypeList;
public UseCaseManageAssetExampleUI(BaseAssetBehaviour behaviour)
{
m_Behaviour = behaviour;
m_AssetTypeList = AssetTypeExtensions.AssetTypeList().ToArray();
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUI
function with the following code:
static GUILayoutOption s_LabelWidth = GUILayout.Width(60);
IAsset m_CurrentAsset;
AssetUpdate m_AssetUpdate;
string m_TagsString = string.Empty;
public void OnGUI()
{
if (m_Behaviour.CurrentAsset == null) return;
if (!m_Behaviour.TryGetAssetProperties(m_Behaviour.CurrentAsset.Descriptor.AssetVersion, out var properties))
{
GUILayout.Label(" ! Asset properties not loaded !");
return;
}
if (!m_Behaviour.CurrentAsset.Equals(m_CurrentAsset))
{
m_CurrentAsset = m_Behaviour.CurrentAsset;
m_AssetUpdate = new AssetUpdate
{
Name = properties.Name,
Tags = properties.Tags?.ToList() ?? new List<string>(),
PreviewFile = properties.PreviewFileDescriptor?.Path ?? "",
Description = properties.Description,
};
m_TagsString = string.Join(',', m_AssetUpdate.Tags);
}
GUILayout.BeginVertical();
if (m_AssetUpdate == null)
{
GUILayout.Label("Loading...");
}
else
{
GUI.enabled = properties.State == AssetState.Unfrozen;
DisplayAsset(properties);
GUI.enabled = true;
}
GUILayout.EndVertical();
}
void DisplayAsset(AssetProperties assetProperties)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Name:", s_LabelWidth);
m_AssetUpdate.Name = GUILayout.TextField(m_AssetUpdate.Name);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Type:", s_LabelWidth);
var typeIndex = m_AssetUpdate.Type.HasValue ? (int) m_AssetUpdate.Type.Value : (int) assetProperties.Type;
typeIndex = GUILayout.SelectionGrid(typeIndex, m_AssetTypeList, 3, GUILayout.ExpandWidth(true));
if (typeIndex != -1 && assetProperties.Type != (AssetType) typeIndex)
{
m_AssetUpdate.Type = (AssetType) typeIndex;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Tags:", s_LabelWidth);
m_TagsString = GUILayout.TextField(m_TagsString);
m_AssetUpdate.Tags = m_TagsString.Split(',')
.Select(tag => tag.Trim())
.Where(tag => !string.IsNullOrEmpty(tag))
.ToList();
GUILayout.EndHorizontal();
if (assetProperties.PreviewFileDescriptor.HasValue)
{
GUILayout.Label("Preview DatasetId: " + assetProperties.PreviewFileDescriptor.Value.DatasetId);
}
else
{
GUILayout.Label("No preview file.");
}
GUILayout.BeginHorizontal();
GUILayout.Label("Preview:", s_LabelWidth);
m_AssetUpdate.PreviewFile = GUILayout.TextField(m_AssetUpdate.PreviewFile);
GUILayout.EndHorizontal();
GUILayout.Label("Description:");
m_AssetUpdate.Description = GUILayout.TextArea(m_AssetUpdate.Description);
if (GUILayout.Button("Update"))
{
_ = m_Behaviour.UpdateAssetAsync(m_AssetUpdate);
}
}
- Open the
AssetManagementUI
script you created as described in Get started with Asset SDK 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 UseCaseManageAssetExampleUI(m_Behaviour));
The UI does the following:
- Displays information about the selected asset.
- Provide fields to update the name, type and tags of the selected asset.