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:
Set up a Unity scene in the Unity Editor with an Organization and Project browser. See Get started with Assets 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 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:
- Open the
AssetManagementBehaviour
script you created. - 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:
- Open the
AssetManagementBehaviour
script you created. - 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:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scripts
folder. - Go to Create > C# Script. Name your script
UseCasePublishWithdrawAssetExampleUI
. - 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() { }
}
- 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);
}
}
- Open the
AssetManagementUI
script you created and replace the contents of theAwake
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.