Use case: Get projects, assets, datasets and files
How do I...?
Get a project
The entry point for the Unity Cloud Assets package is the IAssetRepository
class.
You can get a project like so:
public static async Task<IAssetProject> GetProjectAsync(IAssetRepository assetRepository, string organizationId, string projectId)
{
var projectDescriptor = new ProjectDescriptor(new OrganizationId(organizationId), new ProjectId(projectId));
return await assetRepository.GetAssetProjectAsync(projectDescriptor, CancellationToken.None);
}
Get an asset from a project
Once you have a handle on an IAssetProject
, you can get an asset by its ID, like so:
public static async Task<IAsset> GetAssetAsync(IAssetProject assetProject, string assetId)
{
return await assetProject.GetAssetAsync(new AssetId(assetId), CancellationToken.None);
}
This will return an asset with a default version, usually the latest version.
If you want to get a specific version of an asset, you can do so by providing the version or label, like so:
public static async Task<IAsset> GetAssetAsyncByVersion(IAssetProject assetProject, string assetId, string versionId)
{
return await assetProject.GetAssetAsync(new AssetId(assetId), new AssetVersion(versionId), CancellationToken.None);
}
public static async Task<IAsset> GetAssetAsyncByLabel(IAssetProject assetProject, string assetId, string label)
{
return await assetProject.GetAssetAsync(new AssetId(assetId), label, CancellationToken.None);
}
Get a dataset from an asset
Once you have a handle on an IAsset
, you can get a dataset by its ID, like so:
public static async Task<IDataset> GetAssetAsync(IAsset asset, string datasetId)
{
return await asset.GetDatasetAsync(new DatasetId(datasetId), CancellationToken.None);
}
Get a file from a dataset
Once you have a handle on an IDataset
, you can get a file by its path, like so:
public static async Task<IFile> GetFileAsync(IDataset dataset, string filePath)
{
return await dataset.GetFileAsync(filePath, CancellationToken.None);
}
The above methods traverse the hierarchy of entities in the Unity Cloud Assets package, starting from a project and ending with a file. You can use these methods to get any entity in the hierarchy.
Skip the hierarchy
If you know the IDs of an entity and want to get it directly, you can use methods of the IAssetRepository
class, like so:
public static async Task<IAsset> GetAssetAsync(IAssetRepository assetRepository, string organizationId, string projectId, string assetId, string assetVersion)
{
var projectDescriptor = new ProjectDescriptor(new OrganizationId(organizationId), new ProjectId(projectId));
var assetDescriptor = new AssetDescriptor(projectDescriptor, new AssetId(assetId), new AssetVersion(assetVersion));
return await assetRepository.GetAssetAsync(assetDescriptor, CancellationToken.None);
}
public static async Task<IAsset> GetAssetByLabelAsync(IAssetRepository assetRepository, string organizationId, string projectId, string assetId, string label)
{
var projectDescriptor = new ProjectDescriptor(new OrganizationId(organizationId), new ProjectId(projectId));
return await assetRepository.GetAssetAsync(projectDescriptor, new AssetId(assetId), label, CancellationToken.None);
}
public static async Task<IDataset> GetDatasetAsync(IAssetRepository assetRepository, string organizationId, string projectId, string assetId, string assetVersion, string datasetId)
{
var projectDescriptor = new ProjectDescriptor(new OrganizationId(organizationId), new ProjectId(projectId));
var assetDescriptor = new AssetDescriptor(projectDescriptor, new AssetId(assetId), new AssetVersion(assetVersion));
var datasetDescriptor = new DatasetDescriptor(assetDescriptor, new DatasetId(datasetId));
return await assetRepository.GetDatasetAsync(datasetDescriptor, CancellationToken.None);
}
public static async Task<IFile> GetFileAsync(IAssetRepository assetRepository, string organizationId, string projectId, string assetId, string assetVersion, string datasetId, string filePath)
{
var projectDescriptor = new ProjectDescriptor(new OrganizationId(organizationId), new ProjectId(projectId));
var assetDescriptor = new AssetDescriptor(projectDescriptor, new AssetId(assetId), new AssetVersion(assetVersion));
var datasetDescriptor = new DatasetDescriptor(assetDescriptor, new DatasetId(datasetId));
var fileDescriptor = new FileDescriptor(datasetDescriptor, filePath);
return await assetRepository.GetFileAsync(fileDescriptor, CancellationToken.None);
}
The above methods allow you to get any entity in the hierarchy directly, without traversing the hierarchy.