Create and upload asset files
You can use the Unity Asset Manager SDK package to manage the files linked to an asset.
File management is only available through the Asset Management pathway.
Note: File management requires users have the role of
Asset Management Contributor
OR a minimum role ofManager
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 either Get started with Asset Discovery or Get started with Asset Management 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 Asset Management.
- 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...?
Create an asset file
To create a new asset file, open the AssetManagementBehaviour
script you created and add the following code to the end of the class:
static readonly byte[] s_Bytes = new byte[]
{
100, 100, 100, 100, 100, 100, 100, 100, 100, 100
};
public List<(IAsset, IAssetFile)> NewAssetFiles { get; } = new();
public async Task CreateAssetFile()
{
var fileCreation = new AssetFileCreation
{
Name = CurrentAsset.Name + "_file",
Description = "Documentation example asset file creation.",
Type = nameof(Texture2D),
FileSize = s_Bytes.LongLength,
Tags = new List<string> {"Texture", "Gray"}
};
var cancellationTokenSrc = new CancellationTokenSource();
try
{
var assetFile = await PlatformServices.AssetFileManager.CreateAssetFileAsync(m_CurrentProject, CurrentAsset, fileCreation, cancellationTokenSrc.Token);
if (assetFile != null)
{
NewAssetFiles.Add((CurrentAsset, assetFile));
}
}
catch (Exception e)
{
Debug.LogError($"Failed to create asset file. {e.Message}");
throw;
}
}
The script creates a new asset file on the selected asset from a default Unity texture.
Upload an asset file
To upload the associated asset of an asset file:
- Open the
AssetManagementBehaviour
script you created. - Add the following code to the end of the class:
class LogProgress : IProgress<HttpProgress>
{
public void Report(HttpProgress value)
{
if (!value.UploadProgress.HasValue) return;
Debug.Log($"Upload progress: {value.UploadProgress*100} %");
}
}
public async Task UploadAssetFile(IAsset asset, IAssetFile assetFile)
{
// Uses the same texture as the file creation.
var contentStream = new MemoryStream(s_Bytes);
var cancellationTokenSrc = new CancellationTokenSource();
try
{
var progress = new LogProgress();
var didUpload = await PlatformServices.AssetFileManager.UploadAssetFileAsync(asset.Project, assetFile, contentStream, progress, cancellationTokenSrc.Token);
if (!didUpload)
{
throw new Exception();
}
await PlatformServices.AssetFileManager.FinalizeAssetFileUploadAsync(asset.Project, assetFile, cancellationTokenSrc.Token);
Debug.Log($"Asset file upload: {assetFile.Name} finalized.");
}
catch (Exception e)
{
Debug.LogError($"Failed to upload asset file: {assetFile.Name}. {e.Message}");
}
}
The script does the following:
- Uploads the file content to the cloud.
- Prints the a message to the console when the upload is complete OR prints an error message if the upload fails.
Add the UI for interacting with asset files
To add UI for the example:
- Open the
AssetManagementUI
script you created. - Replace the
AssetActions
function with the following code:
protected virtual void AssetActions()
{
if (m_Behaviour.CurrentAsset == null)
{
GUILayout.Label(" ! No asset selected !");
return;
}
if (GUILayout.Button("Create new asset file"))
{
_ = m_Behaviour.CreateAssetFile();
}
GUILayout.Space(10);
GUILayout.Label("Asset files:");
var assetFiles = m_Behaviour.NewAssetFiles;
for (var i = 0; i < assetFiles.Count; ++i)
{
DisplayAssetFile(assetFiles[i]);
}
}
void DisplayAssetFile((IAsset asset, IAssetFile assetFile) tuple)
{
GUILayout.BeginHorizontal();
GUILayout.Label($"{tuple.assetFile.Name}");
GUILayout.Space(5f);
if (GUILayout.Button("Upload"))
{
_ = m_Behaviour.UploadAssetFile(tuple.asset, tuple.assetFile);
}
GUILayout.EndHorizontal();
}
The script does the following:
- Provides UI buttons to trigger the creation of a new asset file.
- Displays a list of created asset files and provides a UI button to upload the file content.
Going further
For more a more in-depth look at file management, see the Asset Database Uploader sample.