docs.unity3d.com
    Show / Hide Table of Contents

    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 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 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:

    1. Open the AssetManagementBehaviour script you created.
    2. 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:

    1. Open the AssetManagementUI script you created.
    2. 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.

    Back to top
    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