docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Publish and/or withdraw assets

    You can use the Unity Cloud Assets package to publish assets so they are available to viewers, or withdraw assets so they are open for modification.

    Organization or Asset Manager Project role Publish/withdraw
    Asset Management Viewer no
    Asset Management Consumer no
    Asset Management Contributor yes
    Organization Owner yes
    Note

    Asset management requires users have the role of Asset Management Contributor OR a minimum role of Manager in the Organization.

    Before you start

    Before you start, you must:

    1. Set up a Unity scene in the Unity Editor with an Organization and Project browser. See Get started with Assets for more information.

    2. Have some assets in the cloud. There are several ways to do so:

      • You can create assets through the Get started with Assets.
      • 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...?

    Publish an asset

    To publish an asset, follow these steps:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task PublishAsset()
    {
        var cancellationTokenSrc = new CancellationTokenSource(k_DefaultCancellationTimeout);
        try
        {
            await CurrentAsset.UpdateStatusAsync(AssetStatusAction.Publish, cancellationTokenSrc.Token);
        }
        catch (Exception e)
        {
            Debug.LogError($"Failed to publish asset: {CurrentAsset.Name}. {e}");
        }
    }
    
    

    The code snippet sets the asset in the cloud as published.

    Withdraw a published asset

    To withdraw a published asset, follow these steps:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task WithdrawAsset()
    {
        var cancellationTokenSrc = new CancellationTokenSource(k_DefaultCancellationTimeout);
        try
        {
            await CurrentAsset.UpdateStatusAsync(AssetStatusAction.Withdraw, cancellationTokenSrc.Token);
        }
        catch (Exception e)
        {
            Debug.LogError($"Failed to withdraw published asset: {CurrentAsset.Name}. {e}");
        }
    }
    
    

    The code snippet withdraws the published asset.

    Add the UI for updating the status of assets

    To create UI for updating the status of assets, 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 UseCasePublishWithdrawAssetExampleUI.
    4. Open the UseCasePublishWithdrawAssetExampleUI script you created and replace the contents of the file with the following code sample:
    
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Unity.Cloud.Assets;
    using UnityEngine;
    
    public class UseCasePublishWithdrawAssetExampleUI : IAssetManagementUI
    {
        readonly AssetManagementBehaviour m_Behaviour;
    
        public UseCasePublishWithdrawAssetExampleUI(AssetManagementBehaviour behaviour)
        {
            m_Behaviour = behaviour;
        }
    
        public void OnGUI() { }
    }
    
    
    1. In the same script, replace the OnGUI function with the following code:
    
    public void OnGUI()
    {
        if (!m_Behaviour.IsProjectSelected) return;
    
        if (m_Behaviour.CurrentAsset == null)
        {
            GUILayout.Label(" ! No asset selected !");
            return;
        }
    
        if (string.Equals(m_Behaviour.CurrentAsset.Status, "Approved", StringComparison.OrdinalIgnoreCase))
        {
            if (GUILayout.Button("Publish"))
            {
                _ = m_Behaviour.PublishAsset();
            }
    
            GUILayout.Space(5f);
        }
    
        if (string.Equals(m_Behaviour.CurrentAsset.Status, "Published", StringComparison.OrdinalIgnoreCase))
        {
            if (GUILayout.Button("Withdraw"))
            {
                _ = m_Behaviour.WithdrawAsset();
            }
    
            GUILayout.Space(5f);
        }
    }
    
    
    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 UseCasePublishWithdrawAssetExampleUI(m_Behaviour));
    

    The code snippet provides UI buttons to trigger the publish or the withdraw of a new asset.

    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)