View the collections of an asset and manage its associations to these collections
You can use the Unity Asset Manager SDK package to manage the collections of an asset.
Collection management is only available through the Asset Management pathway.
Note: Collection management requires users have the role of
Asset Management ContributorOR a minimum role ofManagerin their organization.
Before you start
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 upload assets from existing Unity assets; see the Asset Database Uploader sample.
- You can create assets through the dashboard; see the Managing assets on the dashboard documentation. ## How do I...?
List the collections of an asset
By default, an asset's collections are not included when you get an asset. To fetch the collections of an asset:
- Open the
AssetManagementBehaviourscript you created. - Add the following code to the end of the class:
public async Task RefreshAssetCollections()
{
var cancellationTokenSrc = new CancellationTokenSource();
await CurrentAsset.RefreshAssetCollectionsAsync(cancellationTokenSrc.Token);
}
The code snippet populates a list of the collections of the selected asset.
Remove an asset from a collection
To remove an asset from a collection:
- Open the
AssetManagementBehaviourscript you created. - Add the following code to the end of the class:
public async Task RemoveAssetFromCollectionAsync(CollectionPath collectionPath)
{
var cancellationTokenSrc = new CancellationTokenSource();
var collection = await CurrentAsset.GetCollectionAsync(collectionPath, cancellationTokenSrc.Token);
if (collection == null)
{
Debug.LogError($"Collection {collectionPath} not found.");
return;
}
await collection.RemoveAssetsAsync(new[] {CurrentAsset}, cancellationTokenSrc.Token);
await RefreshAssetCollections();
Debug.Log("Asset removed from collection.");
}
The code snippet does the following:
- Removes the selected asset from the specified collection.
- Updates the list of collections of the selected asset.
- Prints a message to the console on success.
Add the UI for viewing and creating collections
To add UI for the example:
- Open the
AssetManagementUIscript you created. - Replace the
AssetActionsfunction with the following code:
protected virtual void AssetActions()
{
if (GUILayout.Button("Refresh asset collections"))
{
_ = m_Behaviour.RefreshAssetCollections();
}
GUILayout.Label("Collections:");
if (m_Behaviour.CurrentAsset != null)
{
foreach (var collection in m_Behaviour.CurrentAsset.Collections)
{
DisplayAssetCollections(collection);
}
}
else
{
GUILayout.Label(" ! No asset selected !");
}
}
void DisplayAssetCollections(string collectionName)
{
GUILayout.BeginHorizontal();
GUILayout.Label($"{collectionName}");
if (GUILayout.Button("Remove asset"))
{
_ = m_Behaviour.RemoveAssetFromCollectionAsync(collectionName);
}
GUILayout.EndHorizontal();
}
The code snippet displays:
- A list of the selected asset's collections.
- A UI button beside each collection to remove the selected asset from it.