docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Manage an asset's association to collections

    You can use the Unity Cloud Assets package to:

    • List the collections an asset belongs to.
    • Add and remove an asset from a collection.

    The SDK supports different workflows for users with different roles.

    Organization or Asset Manager Project role List an asset's collections Add/remove assets in collections
    Asset Management Viewer yes no
    Asset Management Consumer yes no
    Asset Management Contributor yes yes
    Organization Owner yes yes

    Before you start

    Before you start, you need some assets in the cloud. There are several ways to do so:

    • You can create assets through the Get started with Assets.
    • 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 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 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 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 list of collections of the selected asset.
    • Prints a message to the console on success.

    Add the UI for viewing and creating collections

    To create UI for displaying asset collections, follow these steps:

    1. In your Unity Project window, go to Assets > Scripts.
    2. Select and hold the Assets/Scripts folder.
    3. Go to Create > C# Script. Name your script UseCaseAssetCollectionExampleUI.
    4. Open the UseCaseAssetCollectionExampleUI script you created 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() { }
    }
    
    
    1. In the same script, replace the OnGUI function 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();
    }
    
    
    1. Open the AssetManagementUI script you created and replace the contents of the Awake 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 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 beside each collection to remove the selected asset from it.
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)