Use case: Update the status of assets
You can use the Unity Cloud Assets package to update the status of assets in the cloud.
Organization or Asset Manager Project role | View status | Update status |
---|---|---|
Asset Management Viewer |
yes | no |
Asset Management Consumer |
yes | no |
Asset Management Contributor |
yes | yes |
Organization Owner |
yes | 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 create assets through the dashboard; see the Managing assets on the dashboard documentation.
How do I...?
View reachable statuses for an asset
To view the next available statuses of an asset, follow these steps:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public string[] ReachableStatuses { get; private set; }
public async Task GetReachableStatuses()
{
ReachableStatuses = null;
ReachableStatuses = await CurrentAsset.GetReachableStatusNamesAsync(default);
}
The code snippet sets the collection of next available statuses for the asset.
Update the status of an asset
To update the status of an asset, follow these steps:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
public async Task UpdateStatusAsync(string reachableStatus)
{
await CurrentAsset.UpdateStatusAsync(reachableStatus, default);
await GetReachableStatuses();
}
Add the UI for viewing and 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
UseCaseManageAssetStatusExample
. - Open the
UseCaseManageAssetStatusExample
script you created and replace the contents of the file with the following code sample:
using System;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using UnityEngine;
public class UseCaseManageAssetStatusExampleUI : IAssetManagementUI
{
readonly AssetManagementBehaviour m_Behaviour;
public UseCaseManageAssetStatusExampleUI(AssetManagementBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- 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.GetReachableStatuses();
}
GUILayout.BeginVertical();
GUILayout.Label($"Current Status: {m_CurrentAsset.StatusName}");
GUILayout.Space(5f);
if (m_Behaviour.ReachableStatuses == null)
{
GUILayout.Label("Reachable Statuses: Loading...");
}
else
{
GUILayout.Label("Reachable Statuses:");
foreach (var status in m_Behaviour.ReachableStatuses)
{
if (GUILayout.Button(status))
{
_ = m_Behaviour.UpdateStatusAsync(status);
}
}
}
GUILayout.EndVertical();
}
- 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 UseCaseManageAssetStatusExampleUI(m_Behaviour));
The code snippet displays the current status of the asset and provides UI buttons to update the asset to a next available status.