Use case: Navigating public asset libraries
You can use the Unity Cloud Assets package to view public asset libraries and their contents, such as assets, datasets, and files. You can also copy assets from public libraries to your own project.
| Organization or Asset Manager Project role | List libraries | List assets/datasets/files in libraries | Download files | Copy assets from libraries to projects |
|---|---|---|---|---|
Asset Management Viewer |
yes | yes | yes | no |
Asset Management Consumer |
yes | yes | yes | no |
Asset Management Contributor |
yes | yes | yes | yes |
Organization Owner |
yes | yes | yes | yes |
Before you start
Before you start, you must:
- Set up a Unity scene in the Unity Editor with an Asset Library browser. See Get started with Asset Libraries for more information.
How do I...?
Download files for assets in public Asset Libraries
If you have not already done so, you must update the behaviour to get the list of files.
See the Use case: Update an asset's files documentation for more information.
List collections in public Asset Libraries
To list the collections of an Asset Library, follow these steps:
- Open the
AssetLibrariesBehaviourscript you created. - Add the following code to the end of the class:
public Dictionary<CollectionPath, AssetCollectionProperties> AssetCollectionProperties { get; } = new();
public CollectionPath? CurrentCollection { get; set; }
public async Task ListAssetCollectionsAsync()
{
var selectedCollection = CurrentCollection;
CurrentCollection = null;
AssetCollectionProperties.Clear();
var results = CurrentAssetLibrary.QueryCollections().ExecuteAsync(CancellationToken.None);
await foreach (var collection in results)
{
var properties = await collection.GetPropertiesAsync(CancellationToken.None);
AssetCollectionProperties.Add(collection.Descriptor.Path, properties);
if (collection.Descriptor.Path == selectedCollection)
{
CurrentCollection = collection.Descriptor.Path;
}
}
}
The code snippet does the following:
- Maintains a list of collections and their properties for the selected Asset Library.
To create UI for listing collections, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
UseCaseListAssetLibraryCollectionsExampleUI. - Open the
UseCaseListAssetLibraryCollectionsExampleUIscript 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 Unity.Cloud.Common;
using UnityEngine;
public class UseCaseListAssetLibraryCollectionsExampleUI : IAssetManagementUI
{
readonly AssetLibrariesBehaviour m_Behaviour;
public UseCaseListAssetLibraryCollectionsExampleUI(AssetLibrariesBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
IAssetLibrary m_CurrentLibrary;
Vector2 m_ListScrollPosition;
public void OnGUI()
{
if (!m_Behaviour.IsAssetLibrarySelected) return;
if (m_Behaviour.CurrentAssetLibrary != m_CurrentLibrary)
{
m_CurrentLibrary = m_Behaviour.CurrentAssetLibrary;
_ = m_Behaviour.ListAssetCollectionsAsync();
return;
}
GUILayout.BeginVertical();
GUILayout.Label($"Library: {m_Behaviour.GetAssetLibraryName(m_Behaviour.CurrentAssetLibrary.Id)}");
// Go back to select a different library.
if (GUILayout.Button("Back"))
{
m_Behaviour.SetSelectedAssetLibrary(null);
return;
}
GUILayout.Space(15f);
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.ListAssetCollectionsAsync();
return;
}
ListCollections();
GUILayout.EndVertical();
GUILayout.BeginVertical();
DrawCurrentCollection();
GUILayout.EndVertical();
}
void ListCollections()
{
GUILayout.BeginVertical();
GUILayout.Space(15f);
GUILayout.Label("Available Collections:");
if (GUILayout.Button("Refresh collection list"))
{
_ = m_Behaviour.ListAssetCollectionsAsync();
}
m_ListScrollPosition = GUILayout.BeginScrollView(m_ListScrollPosition);
// Hold a local reference to the collections to avoid concurrent modification exceptions.
foreach (var kvp in m_Behaviour.AssetCollectionProperties)
{
GUILayout.BeginHorizontal();
DrawCollection(kvp.Key, kvp.Value);
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
}
void DrawCollection(CollectionPath collectionPath, AssetCollectionProperties properties)
{
GUILayout.Label($"{collectionPath.GetLastComponentOfPath()}", GUILayout.MaxWidth(Screen.width * 0.2f));
GUI.enabled = collectionPath != m_Behaviour.CurrentCollection;
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.CurrentCollection = collectionPath;
}
GUI.enabled = true;
}
void DrawCurrentCollection()
{
if (!m_Behaviour.CurrentCollection.HasValue) return;
if (!m_Behaviour.AssetCollectionProperties.TryGetValue(m_Behaviour.CurrentCollection.Value, out var properties))
{
GUILayout.Label(" ! Collection properties not loaded !");
return;
}
var collectionPath = m_Behaviour.CurrentCollection.Value;
GUILayout.BeginVertical();
GUILayout.Label($"{collectionPath.GetParentPath()}::{collectionPath.GetLastComponentOfPath()}");
GUILayout.Label("Name: ");
GUILayout.Label(collectionPath.GetLastComponentOfPath());
GUILayout.Label("Description: ");
GUILayout.Label(properties.Description, GUILayout.MinHeight(60));
GUILayout.EndVertical();
}
- Open the
AssetLibrariesBehaviourscript you created and replace the contents of theAwakefunction with the following code:
m_UI.Add(new AssetLibrarySelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseListAssetLibraryCollectionsExampleUI(m_Behaviour));
The code snippet does the following:
- Displays a list of collections in the selected Asset Library. Each collection has a UI button to select it and display its properties.
List labels in public Asset Libraries
To list the labels of an Asset Library, follow these steps:
- Open the
AssetLibrariesBehaviourscript you created. - Add the following code to the end of the class:
public List<ILabel> Labels { get; } = new();
public List<ILabel> ArchivedLabels { get; } = new();
public Dictionary<string, LabelProperties> LabelProperties { get; } = new();
public string CurrentLabelName { get; private set; }
public async Task GetLabels()
{
var currentLabelName = CurrentLabelName;
CurrentLabelName = null;
Labels.Clear();
ArchivedLabels.Clear();
var filter = new LabelSearchFilter();
filter.IsArchived.WhereEquals(false);
var asyncList = CurrentAssetLibrary.QueryLabels()
.SelectWhereMatchesFilter(filter)
.ExecuteAsync(CancellationToken.None);
await foreach (var label in asyncList)
{
Labels.Add(label);
if (label.Descriptor.LabelName == currentLabelName)
{
SetCurrentLabel(currentLabelName);
}
LabelProperties[label.Descriptor.LabelName] = await label.GetPropertiesAsync(CancellationToken.None);
}
filter.IsArchived.WhereEquals(true);
asyncList = CurrentAssetLibrary.QueryLabels()
.SelectWhereMatchesFilter(filter)
.ExecuteAsync(CancellationToken.None);
await foreach (var label in asyncList)
{
ArchivedLabels.Add(label);
if (label.Descriptor.LabelName == currentLabelName)
{
SetCurrentLabel(currentLabelName);
}
LabelProperties[label.Descriptor.LabelName] = await label.GetPropertiesAsync(CancellationToken.None);
}
}
public void SetCurrentLabel(string labelName)
{
CurrentLabelName = labelName;
}
The code snippet does the following:
- Maintains a list of labels and their properties for the selected Asset Library.
To create UI for listing labels, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
UseCaseListAssetLibraryLabelsExampleUI. - Open the
UseCaseListAssetLibraryLabelsExampleUIscript you created and replace the contents of the file with the following code sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using Unity.Cloud.Common;
using UnityEngine;
public class UseCaseListAssetLibraryLabelsExampleUI : IAssetManagementUI
{
readonly AssetLibrariesBehaviour m_Behaviour;
public UseCaseListAssetLibraryLabelsExampleUI(AssetLibrariesBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
IAssetLibrary m_CurrentLibrary;
Vector2 m_ListScrollPosition;
public void OnGUI()
{
if (!m_Behaviour.IsAssetLibrarySelected) return;
if (m_CurrentLibrary != m_Behaviour.CurrentAssetLibrary)
{
m_CurrentLibrary = m_Behaviour.CurrentAssetLibrary;
_ = m_Behaviour.GetLabels();
return;
}
GUILayout.BeginVertical();
GUILayout.Label($"Library: {m_Behaviour.GetAssetLibraryName(m_Behaviour.CurrentAssetLibrary.Id)}");
// Go back to select a different scene.
if (GUILayout.Button("Back"))
{
m_Behaviour.SetSelectedAssetLibrary(null);
return;
}
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.GetLabels();
return;
}
ListLabels();
GUILayout.EndVertical();
GUILayout.BeginVertical();
if (!string.IsNullOrEmpty(m_Behaviour.CurrentLabelName))
{
DisplayLabel();
}
GUILayout.EndVertical();
}
void ListLabels()
{
if (m_Behaviour.Labels == null || m_Behaviour.ArchivedLabels == null) return;
if (m_Behaviour.Labels.Count == 0 && m_Behaviour.ArchivedLabels.Count == 0)
{
GUILayout.Label(" ! No labels !");
}
else
{
m_ListScrollPosition = GUILayout.BeginScrollView(m_ListScrollPosition, GUILayout.MinWidth(Screen.width * 0.3f), GUILayout.Height(Screen.height * 0.8f));
var labels = m_Behaviour.Labels.ToArray();
foreach (var label in labels)
{
DisplayLabel(label.Descriptor.LabelName, () => { });
}
var archivedLabels = m_Behaviour.ArchivedLabels.ToArray();
foreach (var archivedLabel in archivedLabels)
{
DisplayLabel(archivedLabel.Descriptor.LabelName, () => { });
}
GUILayout.EndScrollView();
}
}
void DisplayLabel(string labelName, Action action)
{
GUILayout.BeginHorizontal();
GUILayout.Label(labelName);
GUI.enabled = labelName != m_Behaviour.CurrentLabelName;
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.SetCurrentLabel(labelName);
}
GUI.enabled = true;
if (m_Behaviour.LabelProperties.TryGetValue(labelName, out var properties))
{
if (!properties.IsSystemLabel)
{
action?.Invoke();
}
}
GUILayout.EndHorizontal();
}
void DisplayLabel()
{
if (!m_Behaviour.LabelProperties.TryGetValue(m_Behaviour.CurrentLabelName, out var properties))
{
GUILayout.Label(" ! Label properties not loaded !");
return;
}
var isArchived = m_Behaviour.ArchivedLabels.Any(l => l.Descriptor.LabelName == m_Behaviour.CurrentLabelName);
GUILayout.Label($"Label: {m_Behaviour.CurrentLabelName}" + (isArchived ? " (archived)" : string.Empty));
GUILayout.Label($"Is system: {properties.IsSystemLabel}");
GUILayout.Label($"Is assignable: {properties.IsAssignable}");
GUILayout.Label($"Created on: {properties.AuthoringInfo?.Created:yyyy-M-d dddd}");
GUILayout.Label($"Updated on: {properties.AuthoringInfo?.Updated:yyyy-M-d dddd}");
GUILayout.Label($"Description: {properties.Description}");
GUILayout.Label($"Color: {properties.DisplayColor?.Name ?? "None"}");
}
- Open the
AssetLibrariesBehaviourscript you created and replace the contents of theAwakefunction with the following code:
m_UI.Add(new AssetLibrarySelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseListAssetLibraryLabelsExampleUI(m_Behaviour));
The code snippet does the following:
- Displays a list of labels in the selected Asset Library. Each label has a UI button to select it and display its properties.
List field definitions for assets in public Asset Libraries
To list the field definitions of an asset in an Asset Library, follow these steps:
- Open the
AssetLibrariesBehaviourscript you created. - Add the following code to the end of the class:
public Dictionary<string, FieldDefinitionProperties> FieldDefinitionProperties { get; } = new();
public string CurrentFieldDefinitionKey { get; private set; }
public FieldDefinitionUpdate FieldDefinitionUpdate { get; private set; }
public async Task GetFieldDefinitionsAsync()
{
var fieldKey = CurrentFieldDefinitionKey;
CurrentFieldDefinitionKey = null;
FieldDefinitionProperties.Clear();
var metadataQuery = CurrentAsset.Metadata.Query().SelectAll().ExecuteAsync(CancellationToken.None);
var metadataKeys = new List<string>();
await foreach (var kvp in metadataQuery)
{
metadataKeys.Add(kvp.Key);
}
var asyncList = CurrentAssetLibrary.QueryFieldDefinitions(metadataKeys).ExecuteAsync(CancellationToken.None);
await foreach (var fieldDefinition in asyncList)
{
var properties = await fieldDefinition.GetPropertiesAsync(CancellationToken.None);
FieldDefinitionProperties.Add(fieldDefinition.Descriptor.FieldKey, properties);
if (fieldDefinition.Descriptor.FieldKey == fieldKey)
{
SetCurrentFieldDefinition(fieldKey);
}
}
}
public void SetCurrentFieldDefinition(string fieldDefinitionKey)
{
CurrentFieldDefinitionKey = fieldDefinitionKey;
if (!string.IsNullOrEmpty(CurrentFieldDefinitionKey) && FieldDefinitionProperties.TryGetValue(CurrentFieldDefinitionKey, out var properties))
{
FieldDefinitionUpdate = new FieldDefinitionUpdate {DisplayName = properties.DisplayName};
}
else
{
FieldDefinitionUpdate = null;
}
}
The code snippet does the following:
- Maintains a list of field definitions and their properties for the selected Asset Library.
To create UI for listing field definitions, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
UseCaseListAssetLibraryFieldDefinitionsExampleUI. - Open the
UseCaseListAssetLibraryFieldDefinitionsExampleUIscript you created and replace the contents of the file with the following code sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using UnityEngine;
public class UseCaseListAssetLibraryFieldDefinitionsExampleUI : IAssetManagementUI
{
readonly AssetLibrariesBehaviour m_Behaviour;
public UseCaseListAssetLibraryFieldDefinitionsExampleUI(AssetLibrariesBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
IAsset m_CurrentAsset;
List<string> m_SelectionAcceptedValues = new();
Vector2 m_ListScrollPosition;
public void OnGUI()
{
if (m_Behaviour.CurrentAsset == null) return;
if (m_CurrentAsset != m_Behaviour.CurrentAsset)
{
m_CurrentAsset = m_Behaviour.CurrentAsset;
_ = m_Behaviour.GetFieldDefinitionsAsync();
return;
}
GUILayout.BeginVertical();
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.GetFieldDefinitionsAsync();
}
GUILayout.Space(15f);
GUILayout.Label("Fields:");
ListFieldDefinitions();
GUILayout.EndVertical();
GUILayout.BeginVertical();
if (m_Behaviour.CurrentFieldDefinitionKey != null)
{
DisplayFieldDefinition();
}
GUILayout.EndVertical();
}
void ListFieldDefinitions()
{
if (m_Behaviour.FieldDefinitionProperties.Count == 0)
{
GUILayout.Label(" ! No fields !");
return;
}
m_ListScrollPosition = GUILayout.BeginScrollView(m_ListScrollPosition, GUILayout.MinWidth(Screen.width * 0.2f), GUILayout.Height(Screen.height * 0.8f));
foreach (var kvp in m_Behaviour.FieldDefinitionProperties)
{
GUILayout.BeginHorizontal();
GUILayout.Label(kvp.Key);
GUI.enabled = kvp.Key != m_Behaviour.CurrentFieldDefinitionKey;
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.SetCurrentFieldDefinition(kvp.Key);
if (kvp.Value.Type == FieldDefinitionType.Selection)
{
m_SelectionAcceptedValues = kvp.Value.AsSelectionFieldDefinitionProperties().AcceptedValues?.ToList() ?? new List<string>();
}
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
void DisplayFieldDefinition()
{
if (!m_Behaviour.FieldDefinitionProperties.TryGetValue(m_Behaviour.CurrentFieldDefinitionKey, out var properties))
{
GUILayout.Label(" ! Field definition properties not loaded !");
return;
}
GUILayout.Label($"Field Definition: {m_Behaviour.CurrentFieldDefinitionKey}");
GUILayout.Label(properties.IsDeleted ? "Deleted" : "Active");
GUILayout.Label($"Created on: {properties.AuthoringInfo?.Created:yyyy-M-d dddd}");
GUILayout.Label($"Updated on: {properties.AuthoringInfo?.Updated:yyyy-M-d dddd}");
var multiSelectionStatus = string.Empty;
var acceptedValues = string.Empty;
if (properties.Type == FieldDefinitionType.Selection)
{
var selectionProperties = properties.AsSelectionFieldDefinitionProperties();
multiSelectionStatus = selectionProperties.Multiselection ? ", Multi" : ", Single";
acceptedValues = string.Join(',', selectionProperties.AcceptedValues ?? new List<string>());
}
GUILayout.Label($"Type: {properties.Type}{multiSelectionStatus}");
if (properties.IsDeleted)
{
GUILayout.Label($"Display name: {properties.DisplayName}");
if (!string.IsNullOrEmpty(acceptedValues))
{
GUILayout.Label($"Accepted values: {acceptedValues}");
}
return;
}
GUILayout.Space(5f);
DisplayUpdateValues(m_Behaviour.FieldDefinitionUpdate);
if (properties.Type == FieldDefinitionType.Selection)
{
DisplaySelectionValues();
}
}
static void DisplayUpdateValues(FieldDefinitionUpdate update)
{
GUILayout.Label("Display name:");
update.DisplayName = GUILayout.TextField(update.DisplayName);
}
void DisplaySelectionValues()
{
GUILayout.Space(5f);
GUILayout.Label("Accepted Values:");
var value = string.Join(',', m_SelectionAcceptedValues);
var newValue = GUILayout.TextField(value);
if (value != newValue)
{
m_SelectionAcceptedValues = newValue.Split(',').Select(x => x.Trim()).ToList();
}
}
- Open the
AssetLibrariesBehaviourscript you created and replace the contents of theAwakefunction with the following code:
m_UI.Add(new AssetLibrarySelectionExampleUI(m_Behaviour));
m_UI.Add(new AssetSelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseListAssetLibraryFieldDefinitionsExampleUI(m_Behaviour));
The code snippet does the following:
- Displays a list of assets in the selected Asset Library. Each asset has a UI button to select it and display its properties.
- Displays a list of field definitions in the selected Asset Library. Each field definition has a UI button to select it and display its properties.
List jobs actively copying assets from public Asset Libraries to your project
To create UI for listing Asset Library jobs, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
AssetLibraryJobSelectionExampleUI. - Open the
AssetLibraryJobSelectionExampleUIscript you created and replace the contents of the file with the following code sample:
using UnityEngine;
public class AssetLibraryJobSelectionExampleUI : IAssetManagementUI
{
readonly AssetLibrariesBehaviour m_Behaviour;
Vector2 m_ListScrollPosition;
public AssetLibraryJobSelectionExampleUI(AssetLibrariesBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI()
{
// Refresh the library list
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.GetAssetLibraryJobsAsync();
return;
}
GUILayout.Space(15f);
GUILayout.BeginVertical();
GUILayout.Label("Available Library Jobs:");
GUILayout.Space(5f);
ListAssetLibraryJobs();
GUILayout.EndVertical();
}
void ListAssetLibraryJobs()
{
var jobs = m_Behaviour.AvailableAssetLibraryJobs.ToArray();
if (jobs.Length == 0)
{
GUILayout.Label("No jobs found.");
return;
}
m_ListScrollPosition = GUILayout.BeginScrollView(m_ListScrollPosition, GUILayout.ExpandHeight(true), GUILayout.Width(250));
foreach (var job in jobs)
{
GUI.enabled = job.Id != m_Behaviour.CurrentAssetLibraryJob?.Id;
if (GUILayout.Button(m_Behaviour.GetAssetLibraryJobName(job.Id)))
{
m_Behaviour.SetSelectedAssetLibraryJob(job);
}
GUI.enabled = true;
}
GUILayout.EndScrollView();
}
}
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
UseCaseViewAssetLibraryJobExampleUI. - Open the
UseCaseViewAssetLibraryJobExampleUIscript you created and replace the contents of the file with the following code sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using UnityEngine;
public class UseCaseListAssetLibraryFieldDefinitionsExampleUI : IAssetManagementUI
{
readonly AssetLibrariesBehaviour m_Behaviour;
public UseCaseListAssetLibraryFieldDefinitionsExampleUI(AssetLibrariesBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
IAsset m_CurrentAsset;
List<string> m_SelectionAcceptedValues = new();
Vector2 m_ListScrollPosition;
public void OnGUI()
{
if (m_Behaviour.CurrentAsset == null) return;
if (m_CurrentAsset != m_Behaviour.CurrentAsset)
{
m_CurrentAsset = m_Behaviour.CurrentAsset;
_ = m_Behaviour.GetFieldDefinitionsAsync();
return;
}
GUILayout.BeginVertical();
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.GetFieldDefinitionsAsync();
}
GUILayout.Space(15f);
GUILayout.Label("Fields:");
ListFieldDefinitions();
GUILayout.EndVertical();
GUILayout.BeginVertical();
if (m_Behaviour.CurrentFieldDefinitionKey != null)
{
DisplayFieldDefinition();
}
GUILayout.EndVertical();
}
void ListFieldDefinitions()
{
if (m_Behaviour.FieldDefinitionProperties.Count == 0)
{
GUILayout.Label(" ! No fields !");
return;
}
m_ListScrollPosition = GUILayout.BeginScrollView(m_ListScrollPosition, GUILayout.MinWidth(Screen.width * 0.2f), GUILayout.Height(Screen.height * 0.8f));
foreach (var kvp in m_Behaviour.FieldDefinitionProperties)
{
GUILayout.BeginHorizontal();
GUILayout.Label(kvp.Key);
GUI.enabled = kvp.Key != m_Behaviour.CurrentFieldDefinitionKey;
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.SetCurrentFieldDefinition(kvp.Key);
if (kvp.Value.Type == FieldDefinitionType.Selection)
{
m_SelectionAcceptedValues = kvp.Value.AsSelectionFieldDefinitionProperties().AcceptedValues?.ToList() ?? new List<string>();
}
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
void DisplayFieldDefinition()
{
if (!m_Behaviour.FieldDefinitionProperties.TryGetValue(m_Behaviour.CurrentFieldDefinitionKey, out var properties))
{
GUILayout.Label(" ! Field definition properties not loaded !");
return;
}
GUILayout.Label($"Field Definition: {m_Behaviour.CurrentFieldDefinitionKey}");
GUILayout.Label(properties.IsDeleted ? "Deleted" : "Active");
GUILayout.Label($"Created on: {properties.AuthoringInfo?.Created:yyyy-M-d dddd}");
GUILayout.Label($"Updated on: {properties.AuthoringInfo?.Updated:yyyy-M-d dddd}");
var multiSelectionStatus = string.Empty;
var acceptedValues = string.Empty;
if (properties.Type == FieldDefinitionType.Selection)
{
var selectionProperties = properties.AsSelectionFieldDefinitionProperties();
multiSelectionStatus = selectionProperties.Multiselection ? ", Multi" : ", Single";
acceptedValues = string.Join(',', selectionProperties.AcceptedValues ?? new List<string>());
}
GUILayout.Label($"Type: {properties.Type}{multiSelectionStatus}");
if (properties.IsDeleted)
{
GUILayout.Label($"Display name: {properties.DisplayName}");
if (!string.IsNullOrEmpty(acceptedValues))
{
GUILayout.Label($"Accepted values: {acceptedValues}");
}
return;
}
GUILayout.Space(5f);
DisplayUpdateValues(m_Behaviour.FieldDefinitionUpdate);
if (properties.Type == FieldDefinitionType.Selection)
{
DisplaySelectionValues();
}
}
static void DisplayUpdateValues(FieldDefinitionUpdate update)
{
GUILayout.Label("Display name:");
update.DisplayName = GUILayout.TextField(update.DisplayName);
}
void DisplaySelectionValues()
{
GUILayout.Space(5f);
GUILayout.Label("Accepted Values:");
var value = string.Join(',', m_SelectionAcceptedValues);
var newValue = GUILayout.TextField(value);
if (value != newValue)
{
m_SelectionAcceptedValues = newValue.Split(',').Select(x => x.Trim()).ToList();
}
}
- Open the
AssetLibrariesBehaviourscript you created and replace the contents of theAwakefunction with the following code:
m_UI.Add(new AssetLibraryJobSelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseViewAssetLibraryJobExampleUI(m_Behaviour));
The code snippets do the following:
- Display a list of jobs for the selected Asset Library. Each job has a UI button to select it and display its properties.
Copy assets from public Asset Libraries to your project
To copy assets from a public Asset Library to your project, follow these steps:
- Open the
AssetLibrariesBehaviourscript you created. - Add the following code to the end of the class:
public async Task StartAssetLibraryJobAsync(CollectionDescriptor collectionDescriptor)
{
var list = new AssetsToCopy();
list.Add(CurrentAsset.Descriptor, collectionDescriptor);
try
{
await foreach (var jobResult in CurrentAssetLibrary.StartCopyAssetsJobAsync(collectionDescriptor.ProjectDescriptor, list, CancellationToken.None))
{
Debug.Log($"Started copy asset job with id {jobResult.Id} for asset {CurrentAsset.Descriptor.AssetId}.");
}
}
catch (ServiceException e)
{
Debug.LogError(e.Message);
}
}
The code snippet does the following:
- Starts a job on the selected Asset Library to copy the selected asset.
To create UI for copying assets, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script. Name your script
UseCaseStartAssetLibraryJobExampleUI. - Open the
UseCaseStartAssetLibraryJobExampleUIscript 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 Unity.Cloud.Common;
using UnityEngine;
public class UseCaseStartAssetLibraryJobExampleUI : IAssetManagementUI
{
readonly AssetLibrariesBehaviour m_Behaviour;
public UseCaseStartAssetLibraryJobExampleUI(AssetLibrariesBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
string m_OrganizationId;
string m_ProjectId;
string m_CollectionPath;
public void OnGUI()
{
if (!m_Behaviour.IsAssetLibrarySelected) return;
if (m_Behaviour.CurrentAsset == null)
{
GUILayout.Label("! No asset selected. !");
return;
}
GUILayout.BeginVertical();
GUILayout.Label("Provide the destination where the asset will be copied to");
m_OrganizationId = AddTextField("Organization ID:", m_OrganizationId);
m_ProjectId = AddTextField("Project ID:", m_ProjectId);
m_CollectionPath = AddTextField("Collection Path:", m_CollectionPath);
GUI.enabled = !string.IsNullOrEmpty(m_OrganizationId) && !string.IsNullOrEmpty(m_ProjectId);
if (GUILayout.Button("Copy Selected Asset"))
{
var projectDescriptor = new ProjectDescriptor(new OrganizationId(m_OrganizationId), new ProjectId(m_ProjectId));
var collectionDescriptor = new CollectionDescriptor(projectDescriptor, m_CollectionPath);
_ = m_Behaviour.StartAssetLibraryJobAsync(collectionDescriptor);
}
GUI.enabled = true;
GUILayout.EndVertical();
}
static string AddTextField(string label, string value, int width = 200)
{
GUILayout.BeginHorizontal();
GUILayout.Label(label, GUILayout.Width(150));
value = GUILayout.TextField(value, GUILayout.Width(width));
GUILayout.EndHorizontal();
return value;
}
- Open the
AssetLibrariesBehaviourscript you created and replace the contents of theAwakefunction with the following code:
m_UI.Add(new AssetLibraryJobSelectionExampleUI(m_Behaviour));
m_UI.Add(new AssetSelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseStartAssetLibraryJobExampleUI(m_Behaviour));
The code snippet does the following:
- Displays a list of assets in the selected Asset Library. Each asset has a UI button to select it.
- Displays the necessary fields to start a job to copy the selected asset from the Asset Library to your project.