Get started with Public Asset Libraries
Asset Manager is a Unity Cloud service that allows you to access public libraries of assets in the cloud. Use Assets SDK to perform the following:
- Read an asset library, its assets, datasets, and files.
- Download files.
- Search your assets in a library based on a set of criteria.
- Group and count assets based on a set of criteria.
- Copy assets from a public library to your asset project.
- Read the collections, field definitions, and labels available in a library.
This section explains how to set up a basic scene and script to initialize and use the Unity Assets package with Asset Manager. The script performs a search for all assets of the selected library and displays the results in a GUI.
Before you begin, verify you meet the prerequisites.
Requirements
To use Assets SDK, you must have a minimum role of Asset Manager Viewer in your Unity Cloud project.
Integrate the package in a Unity project
To integrate the Unity Cloud Assets package in a Unity project, do the following:
- Set up a Unity scene
- Create an
AssetLibraryManager
- Create the
PlatformServices
- Create the base behavior for managing assets
- Create the behavior for managing library assets
- Create the base MonoBehaviour for the UI
- Create an interface for all UI scripts
Set up a Unity scene
To set up a Unity scene, follow these steps:
- In the Project window of the Unity Editor, go to Assets > Scenes.
- Select and hold the
Assets/Scenes
folder. - Go to Create > Scene.
- Name the new scene
AssetLibrariesExample
.
Create an AssetLibraryManager
To create an AssetLibraryManager
object, first create a MonoBehaviour
class to manage the UI and then create the AssetLibraryManager
object in your scene as follows:
- Create a
MonoBehaviour
class to manage the UI: - Create the
AssetLibraryManager
object in your scene:
Create the PlatformServices
If you have not already done so, you must set up the PlatformServices
class to manage the platform services that Assets SDK uses to access the Asset Manager service.
See the Get started with Asset SDK documentation for more information.
Create the base behavior for managing assets
If you have not already done so, you must create a base behavior for asset management. This base behavior provides the basic functionality to search and list assets in a library.
See the Get started with Asset SDK documentation for more information.
Create the behavior for managing library assets
To create the behavior for managing library assets, follow these steps:
- In the Project window of the Unity Editor, go to Assets > Scripts.
- Select and hold the
Assets/Scripts
folder. - Go to Create > C# Script.
- Name your script
AssetLibrariesBehaviour
. - Open the file and replace its contents 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 AssetLibrariesBehaviour : BaseAssetBehaviour
{
public override bool CanSelectAsset => IsAssetLibrarySelected;
public List<IAssetLibrary> AvailableLibraries { get; } = new();
public IAssetLibrary CurrentAssetLibrary { get; private set; }
public bool IsAssetLibrarySelected => CurrentAssetLibrary != null;
public List<IAssetLibraryJob> AvailableAssetLibraryJobs { get; } = new();
public IAssetLibraryJob CurrentAssetLibraryJob { get; private set; }
public bool IsAssetLibraryJobSelected => CurrentAssetLibraryJob != null;
readonly Dictionary<AssetLibraryId, AssetLibraryProperties> m_AssetLibraryProperties = new();
readonly Dictionary<AssetLibraryJobId, AssetLibraryJobProperties> m_AssetLibraryJobProperties = new();
CancellationTokenSource m_LibraryCancellationTokenSrc = new();
CancellationTokenSource m_JobCancellationTokenSrc = new();
public override void Clear()
{
base.Clear();
if (m_LibraryCancellationTokenSrc != null)
{
m_LibraryCancellationTokenSrc.Cancel();
m_LibraryCancellationTokenSrc.Dispose();
}
if (m_JobCancellationTokenSrc != null)
{
m_JobCancellationTokenSrc.Cancel();
m_JobCancellationTokenSrc.Dispose();
}
CurrentAssetLibrary = null;
m_AssetLibraryProperties.Clear();
AvailableLibraries.Clear();
CurrentAssetLibraryJob = null;
m_AssetLibraryJobProperties.Clear();
AvailableAssetLibraryJobs.Clear();
}
public override void ClearParentSelection()
{
SetSelectedAssetLibrary(null);
}
public async Task GetAssetLibrariesAsync()
{
var cancellationToken = GetLibraryListCancellationToken();
var libraryId = CurrentAssetLibrary?.Id;
CurrentAssetLibrary = null;
m_AssetLibraryProperties.Clear();
AvailableLibraries.Clear();
var asyncList = PlatformServices.AssetRepository.ListAssetLibrariesAsync(Range.All, cancellationToken);
await foreach (var assetLibrary in asyncList)
{
AvailableLibraries.Add(assetLibrary);
var properties = await assetLibrary.GetPropertiesAsync(cancellationToken);
m_AssetLibraryProperties.Add(assetLibrary.Id, properties);
if (assetLibrary.Id == libraryId)
{
SetSelectedAssetLibrary(assetLibrary);
}
}
}
public void SetSelectedAssetLibrary(IAssetLibrary assetLibrary)
{
CurrentAsset = null;
CurrentAssetLibrary = assetLibrary;
if (CurrentAssetLibrary != null)
{
Debug.Log("Selected library: " + assetLibrary.Id);
_ = GetAssetCount(assetLibrary);
_ = GetAssetsAsync(assetLibrary.QueryAssets());
}
}
public string GetAssetLibraryName(AssetLibraryId assetLibraryId)
{
return m_AssetLibraryProperties.TryGetValue(assetLibraryId, out var properties) ? properties.Name : assetLibraryId.ToString();
}
public async Task GetAssetLibraryJobsAsync()
{
var cancellationToken = GetJobListCancellationToken();
var jobId = CurrentAssetLibraryJob?.Id;
CurrentAssetLibraryJob = null;
m_AssetLibraryJobProperties.Clear();
AvailableAssetLibraryJobs.Clear();
var asyncList = PlatformServices.AssetRepository.ListAssetLibraryJobsAsync(Range.All, cancellationToken);
await foreach (var job in asyncList)
{
AvailableAssetLibraryJobs.Add(job);
var properties = await job.GetPropertiesAsync(cancellationToken);
m_AssetLibraryJobProperties.Add(job.Id, properties);
if (job.Id == jobId)
{
SetSelectedAssetLibraryJob(job);
}
}
}
public void SetSelectedAssetLibraryJob(IAssetLibraryJob assetLibraryJob)
{
CurrentAssetLibraryJob = assetLibraryJob;
if (CurrentAssetLibrary != null)
{
Debug.Log("Selected library job: " + assetLibraryJob.Id);
}
}
public string GetAssetLibraryJobName(AssetLibraryJobId assetLibraryJobId)
{
return m_AssetLibraryJobProperties.TryGetValue(assetLibraryJobId, out var properties) ? properties.Name : assetLibraryJobId.ToString();
}
public bool TryGetAssetLibraryJobProperties(AssetLibraryJobId assetLibraryJobId, out AssetLibraryJobProperties properties)
{
return m_AssetLibraryJobProperties.TryGetValue(assetLibraryJobId, out properties);
}
public void IncludeProperties(AssetLibraryJobId assetLibraryJobId, AssetLibraryJobProperties properties)
{
m_AssetLibraryJobProperties[assetLibraryJobId] = properties;
}
async Task GetAssetCount(IAssetLibrary assetLibrary)
{
AssetCount = await assetLibrary.CountAssetsAsync(CancellationToken.None);
}
CancellationToken GetLibraryListCancellationToken()
{
if (m_LibraryCancellationTokenSrc != null)
{
m_LibraryCancellationTokenSrc.Cancel();
m_LibraryCancellationTokenSrc.Dispose();
}
m_LibraryCancellationTokenSrc = new CancellationTokenSource();
return m_LibraryCancellationTokenSrc.Token;
}
CancellationToken GetJobListCancellationToken()
{
if (m_JobCancellationTokenSrc != null)
{
m_JobCancellationTokenSrc.Cancel();
m_JobCancellationTokenSrc.Dispose();
}
m_JobCancellationTokenSrc = new CancellationTokenSource();
return m_JobCancellationTokenSrc.Token;
}
}
This script does the following:
- Inherits from the
BaseAssetBehaviour
class that you created here. - Provides the functions to list and select libraries.
Create the base MonoBehaviour for the UI
If you have not already done so, you must create a base MonoBehaviour
for the UI. This base MonoBehaviour
provides the basic functionality to manage the UI for asset management.
See the Get started with Asset SDK documentation for more information.
Create an interface for all UI scripts
If you have not already done so, you must create an interface for all UI scripts. This interface provides a common contract for all UI scripts to implement, ensuring consistency in how they interact with the asset management system.
See the Get started with Asset SDK documentation for more information.
How do I...?
Select a library
To create a UI for selecting a library, do the following:
- In the Project window of the Unity Editor, go to Assets > Scripts.
- Select and hold the
Assets/Scripts
folder. - Go to Create > C# Script.
- Name your script
LibrarySelectionExampleUI
. - Open the file and replace its contents with the following code sample:
It looks like the sample you are looking for does not exist.
This script generates a UI to list and select libraries.
Select an asset
If you have not already done so, you must create the UI to list and select assets.
See the Get started with Asset SDK documentation for more information.
View an asset
If you have not already done so, you must create the UI to display the details of the selected asset.
See the Get started with Asset SDK documentation for more information.
Integrate the UI scripts
To bring all your UI scripts into a single MonoBehaviour
, open the AssetLibrariesUI
script that you created here and replace the contents of the file with the following code sample:
using System;
using Unity.Cloud.Identity;
using UnityEngine;
public class AssetLibrariesUI : BaseAssetUI<AssetLibrariesBehaviour>
{
protected virtual void Awake()
{
m_UI.Add(new AssetLibrarySelectionExampleUI(m_Behaviour));
m_UI.Add(new AssetSelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseViewAssetExampleUI(m_Behaviour));
}
protected override void OnAuthenticationStateChanged(AuthenticationState obj)
{
if (obj == AuthenticationState.LoggedIn)
{
_ = m_Behaviour.GetAssetLibrariesAsync();
_ = m_Behaviour.GetAssetLibraryJobsAsync();
}
}
}
This script does the following:
- Inherits from the
BaseAssetUI
class that you created here. - Registers to the
ICompositeAuthenticator
interface to track sign-in changes. - Creates an instance of the
AssetLibrariesBehaviour
script. - Creates instances of the UI scripts for selecting a library and asset.
Going further
Read more about Navigating public asset libraries.