Use case: Manage an asset's association to collections
You can use the Unity Cloud Assets package to perform the following:
- List the collections to which an asset belongs.
- Add or remove an asset from a collection.
Note
To manage assets, you need the Asset Manager Admin role at the organization level or the Asset Manager Contributor add-on role at the project level. Asset Manager Contributors can manage assets only for the specific projects to which they have access.
Before you start
Before you start, do the following:
- The required permissions. Read more about verifying permissions.
Note
Asset Manager roles define permissions you have for a single Asset Manager project. Depending on your work, permissions can vary across different projects.
Create assets in Unity Cloud any of the following ways:
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, do the following:
- Open the
AssetManagementBehaviourscript that you created as described in Get started with Asset SDK. - Add the following code to the end of the class:
public IEnumerable<CollectionPath> Collections { get; private set; } = Array.Empty<CollectionPath>();
public async Task RefreshAssetCollections()
{
Collections = Array.Empty<CollectionPath>();
var collectionsAsync = CurrentAsset.ListLinkedAssetCollectionsAsync(Range.All, CancellationToken.None);
var collections = new List<CollectionPath>();
await foreach (var collection in collectionsAsync)
{
collections.Add(collection.Path);
}
Collections = collections.ToArray();
}
The code snippet generates the selected asset's collection list.
Remove an asset from a collection
To remove an asset from a collection, do the following:
- Open the
AssetManagementBehaviourscript that you created as described in Get started with Asset SDK. - Add the following code to the end of the class:
public async Task RemoveAssetFromCollectionAsync(CollectionPath collectionPath)
{
var collection = await CurrentProject.GetCollectionAsync(collectionPath, default);
if (collection == null)
{
Debug.LogError($"Collection {collectionPath} not found.");
return;
}
await collection.UnlinkAssetsAsync(new[] {CurrentAsset}, default);
await RefreshAssetCollections();
Debug.Log("Asset removed from collection.");
}
The code snippet does the following:
- Removes the selected asset from the specified collection.
- Updates the selected asset's collection list.
- Displays a success message in the console.
Add a UI for viewing and creating collections
To create a UI for displaying asset collections, 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
UseCaseAssetCollectionExampleUI. - Open the
UseCaseAssetCollectionExampleUIscript that 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.Threading;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using UnityEngine;
public class UseCaseAssetCollectionExampleUI : IAssetManagementUI
{
readonly AssetManagementBehaviour m_Behaviour;
public UseCaseAssetCollectionExampleUI(AssetManagementBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
IAsset m_CurrentAsset;
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.RefreshAssetCollections();
}
GUILayout.BeginVertical();
if (GUILayout.Button("Refresh asset collections"))
{
_ = m_Behaviour.RefreshAssetCollections();
}
GUILayout.Label("Collections:");
if (m_Behaviour.Collections != null)
{
foreach (var collection in m_Behaviour.Collections)
{
DisplayAssetCollections(collection);
}
}
GUILayout.EndVertical();
}
void DisplayAssetCollections(CollectionPath collectionPath)
{
GUILayout.BeginHorizontal();
GUILayout.Label($"{collectionPath}");
if (GUILayout.Button("Remove asset"))
{
_ = m_Behaviour.RemoveAssetFromCollectionAsync(collectionPath);
}
GUILayout.EndHorizontal();
}
- Open the
AssetManagementUIscript that you created as described in Get started with Asset SDK 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 UseCaseAssetCollectionExampleUI(m_Behaviour));
The code snippet displays the following UI elements:
- A UI button to refresh the selected asset's collections.
- A list of the selected asset's collections.
- A UI button next to each collection to remove the selected asset from it.