docs.unity3d.com
    Show / Hide Table of Contents

    Manage 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 of Manager in the organization.

    Before you start

    Before you start, you must:

    1. 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.
    2. 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 Uploader sample.
      • You can create assets through the dashboard; see the Managing assets on the dashboard documentation.

    You should also have uploaded files to an asset; see the Create asset files use case.

    How do I...?

    Getting the asset files of an asset

    By default, an asset's files are not included when you get an asset. To get files associated to an asset:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task RefreshAssetFiles()
    {
        var cancellationTokenSrc = new CancellationTokenSource();
        await PlatformServices.AssetManager.GetAssetDownloadUrlsAsync(CurrentAsset, cancellationTokenSrc.Token);
    }
    
    

    The script populates the Files property of the selected asset.

    Get an asset file's download URL

    To get the download URL of an asset file:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task GetDownloadUrlAsync(IAssetFile assetFile)
    {
        var cancellationTokenSrc = new CancellationTokenSource();
        var downloadUrl = await PlatformServices.AssetFileManager.GetAssetFileUrlAsync(m_CurrentOrganization, m_CurrentProject, assetFile, AssetFileUrlType.Download, cancellationTokenSrc.Token);
        Debug.Log($"Download URL: {downloadUrl}");
    }
    
    

    The script prints the download URL of the specified asset file to the console.

    Update an asset file

    To update an asset file:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task UpdateAssetFile(IAssetFile assetFile)
    {
        var name = assetFile.Name.Split('_');
        var index = int.Parse(name[1]) + 1;
        assetFile.Name = $"{name[0]}_{index}";
    
        var cancellationTokenSrc = new CancellationTokenSource();
        await PlatformServices.AssetFileManager.UpdateAssetFileAsync(m_CurrentOrganization, m_CurrentProject, assetFile, cancellationTokenSrc.Token);
        Debug.Log("File updated.");
    }
    
    

    The script does the following:

    • Increments the index in the name of the asset file.
    • Prints a message to the console on success.

    Delete an asset file

    To delete an asset file:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task DeleteAssetFile(IAssetFile assetFile)
    {
        var cancellationTokenSrc = new CancellationTokenSource();
        await PlatformServices.AssetFileManager.DeleteAssetFileAsync(m_CurrentOrganization, m_CurrentProject, assetFile, cancellationTokenSrc.Token);
        await RefreshAssetFiles();
        Debug.Log("File deleted.");
    }
    
    

    The script does the following:

    • Deletes the asset file.
    • Refreshes the list of files for the selected asset.
    • Prints a message to the console on success.

    Add the UI for interacting with asset files

    To add UI for the example:

    1. Open the AssetManagementUI script you created.
    2. Replace the AssetActions function with the following code:
    
    protected virtual void AssetActions()
    {
        if (GUILayout.Button("Refresh file list"))
        {
            _ = m_Behaviour.RefreshAssetFiles();
        }
    
        GUILayout.Label("Asset files:");
        if (m_Behaviour.CurrentAsset != null)
        {
            // Get a local copy of the list of asset files to avoid concurrent modification exceptions.
            var assetFiles = m_Behaviour.CurrentAsset.Files.ToArray();
            foreach (var assetFile in assetFiles)
            {
                DisplayAssetFile(assetFile);
            }
        }
        else
        {
            GUILayout.Label(" ! No asset selected !");
        }
    }
    
    void DisplayAssetFile(IAssetFile assetFile)
    {
        GUILayout.BeginHorizontal();
    
        GUILayout.Label($"{assetFile.Name}");
        GUILayout.Space(5f);
    
        if (GUILayout.Button("Get download URL"))
        {
            _ = m_Behaviour.GetDownloadUrlAsync(assetFile);
        }
    
        if (GUILayout.Button("Update"))
        {
            _ = m_Behaviour.UpdateAssetFile(assetFile);
        }
    
        if (GUILayout.Button("Delete"))
        {
            _ = m_Behaviour.DeleteAssetFile(assetFile);
        }
    
        GUILayout.EndHorizontal();
    }
    
    

    The script does the following:

    • Displays a list of the selected asset's asset files.
    • Displays UI buttons to update, delete, and output the download URL of each asset file.

    Going further

    For more a more in-depth look at file management, see the Asset database uploader sample.

    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023