docs.unity3d.com
    Show / Hide Table of Contents

    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 Contributor OR a minimum role of Manager in their organization.

    Before you start

    1. 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:

    1. Open the AssetManagementBehaviour script you created.
    2. 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:

    1. Open the AssetManagementBehaviour script you created.
    2. 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:

    1. Open the AssetManagementUI script you created.
    2. Replace the AssetActions function 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.
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023