Manage asset collections in a project
You can use the Unity Asset Manager SDK package to manage your collections of assets.
Collection management is only available through the Asset Management pathway.
Note: Collection management requires users have the role of
Asset Management Contributor
OR a minimum role ofManager
in their organization.
Before you start
Before you start, you must:
- Set up a Unity scene in the Unity Editor with an organization and project browser. See either Get started with Asset Discovery or 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 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 in a project
To list the existing collections in a project:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
The code snippet does the following:
- Populates a list of the collections in the selected project.
- Holds a reference to the selected collection.
Create an asset collection
To create an asset collection:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public async Task CreateAssetCollectionAsync(string name, string parentCollectionPath)
{
var newCollection = new AssetCollection(name, "A collection generated by the use-case example.\nUpdate count: 0", parentCollectionPath);
var cancellationTokenSrc = new CancellationTokenSource();
var path = await PlatformServices.AssetCollectionManager.CreateCollectionAsync(m_CurrentProject, newCollection, cancellationTokenSrc.Token);
// Refresh the list of collections.
await ListProjectAssetCollectionsAsync();
Debug.Log("Collection created at path: " + path);
}
The code snippet does the following:
- Creates a new asset collection with the specified name and parent collection.
- Updates the list of collections in the selected project.
- Prints a message to the console on success.
Add the UI for viewing and creating collections
To add UI for the example:
- Open the
AssetManagementUI
script you created. - Replace the
AssetActions
function with the following code:
string m_NewCollectionName = "My Asset Collection";
string m_ParentCollectionPath = "";
protected virtual void ProjectActions()
{
GUILayout.BeginVertical(GUI.skin.box);
if (GUILayout.Button("Refresh collection list"))
{
_ = m_Behaviour.ListProjectAssetCollectionsAsync();
}
GUILayout.Label("Available Collections:");
if (m_Behaviour.AssetCollections != null)
{
// Hold a local reference to the collections to avoid concurrent modification exceptions.
var collections = m_Behaviour.AssetCollections.ToArray();
foreach (var collection in collections)
{
if (GUILayout.Button($"{collection.Name}"))
{
m_Behaviour.SetCurrentCollection(collection);
}
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical(GUI.skin.box);
GUILayout.Label("Create New Collection");
m_NewCollectionName = TextField(m_NewCollectionName, "Collection Name:");
m_ParentCollectionPath = TextField(m_ParentCollectionPath, "Parent Collection Path:");
if (GUILayout.Button("Create Collection"))
{
try
{
_ = m_Behaviour.CreateAssetCollectionAsync(m_NewCollectionName, m_ParentCollectionPath);
}
catch (Exception e)
{
Debug.LogError(e.Message);
throw;
}
}
GUILayout.EndVertical();
}
static string TextField(string value, string label)
{
GUILayout.BeginHorizontal();
GUILayout.Label(label);
value = GUILayout.TextField(value);
GUILayout.EndHorizontal();
return value;
}
The code snippet does the following:
- Displays a list of the selected project's collections.
- Displays UI buttons and necessary text fields to create a new collection and to select a collection.
Update an asset collection
To update an asset collection:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public async Task UpdateProjectAssetCollectionAsync()
{
var description = CurrentCollection.Description.Split(' ').ToList();
if (int.TryParse(description[^1], out var updateCount))
{
description[^1] = (updateCount + 1).ToString();
}
else
{
description.Add("Update count: 1");
}
var strBuilder = new StringBuilder();
strBuilder.AppendJoin(' ', description);
CurrentCollection.SetDescription(strBuilder.ToString());
var cancellationTokenSrc = new CancellationTokenSource();
await PlatformServices.AssetCollectionManager.UpdateCollectionAsync(CurrentCollection, cancellationTokenSrc.Token);
Debug.Log("Collection updated.");
}
The code snippet does the following:
- Updates the selected collection's description by incrementing a counter within the text.
- Prints a message to the console on success.
Delete an asset collection
To delete an asset collection:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public async Task DeleteProjectAssetCollectionAsync()
{
var cancellationTokenSrc = new CancellationTokenSource();
await PlatformServices.AssetCollectionManager.DeleteCollectionAsync(CurrentCollection, cancellationTokenSrc.Token);
// Refresh the list of collections.
await ListProjectAssetCollectionsAsync();
Debug.Log("Collection deleted.");
}
The code snippet does the following:
- Deletes the selected collection from the project.
- Refreshes the list of collections in the project.
- Prints a message to the console on success.
Move an asset collection
To move an asset collection either to nest it under another collection or re-parent at the root of the project:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public async Task MoveProjectAssetCollectionAsync(string newPath)
{
var cancellationTokenSrc = new CancellationTokenSource();
var result = await PlatformServices.AssetCollectionManager.MoveCollectionToNewPathAsync(CurrentCollection, newPath, cancellationTokenSrc.Token);
await ListProjectAssetCollectionsAsync();
Debug.Log("Collection successfully moved to new path: " + result);
}
The code snippet does the following:
- Moves the selected collection to the specified parent collection.
- Prints a message to the console on success.
Known issues
- Nesting a collection on creation or by moving it results in the collection being unusable. It cannot be moved again or deleted and assets cannot be added or removed from it. This is a known issue and will be fixed in a future release.
Add an asset to a collection
To add an asset to a collection:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public async Task AddAssetToCollectionAsync()
{
if (CurrentAsset == null) return;
var cancellationTokenSrc = new CancellationTokenSource();
await PlatformServices.AssetCollectionManager.InsertAssetsToCollectionAsync(CurrentCollection, new[] {CurrentAsset}, cancellationTokenSrc.Token);
Debug.Log("Asset added to collection.");
}
The code snippet does the following:
- Adds the selected asset to the selected collection.
- Prints a message to the console on success.
Add the UI for interacting with a collection
To add UI for the example:
- Open the
AssetManagementUI
script you created. - Replace the
AssetActions
function with the following code:
string m_NewParentPath = "";
protected virtual void AssetActions()
{
var collection = m_Behaviour.CurrentCollection;
if (collection == null)
{
GUILayout.Label("! No collection selected !");
}
else
{
GUILayout.Label($"{collection.ParentPath}::{collection.Name}");
GUILayout.Space(5f);
GUILayout.Label($"{collection.Description}");
GUILayout.Space(5f);
if (GUILayout.Button("Update"))
{
_ = m_Behaviour.UpdateProjectAssetCollectionAsync();
}
if (GUILayout.Button("Delete"))
{
_ = m_Behaviour.DeleteProjectAssetCollectionAsync();
}
GUILayout.Space(10f);
m_NewParentPath = TextField(m_NewParentPath, "New Parent Path:");
if (GUILayout.Button("Reparent Collection"))
{
_ = m_Behaviour.MoveProjectAssetCollectionAsync(m_NewParentPath);
}
GUILayout.Space(10f);
GUILayout.Label($"Selected Asset: {m_Behaviour.CurrentAsset?.Name ?? "! No asset selected !"}");
if (GUILayout.Button("Add asset to collection"))
{
_ = m_Behaviour.AddAssetToCollectionAsync();
}
}
}
The code snippet does the following:
- Displays UI buttons to update and delete the selected collection.
- Displays a text field and UI button to re-parent the selected collection to another collection.
- Displays a UI button to add the selected asset to the selected collection.