docs.unity3d.com
    Show / Hide Table of Contents

    Search assets

    You can use the Unity Asset Manager SDK package to filter assets in a project based on a set of search criteria.

    There are two workflows when searching for assets which can be controlled from the AssetServiceConfiguration class. Setting the IsDiscovery to true in the AssetServiceConfiguration will fetch and search among published assets only. While setting the value to false will fetch and search among all assets regardless of status.

    The implementation you choose will depend on the project roles of your users.

    Note: The Asset Discovery pathway requires users have the minimum role of Asset Management Viewer, while the Asset Management requires higher permissions with a minimum role of Asset Management Contributor.

    How do I...?

    Built-in Search Filter

    The Unity Asset Manager SDK package provides a built-in search filter that you can use to search for assets: the AssetSearchFilter class. The AssetSearchFilter class provides the set of properties that can be used to filter assets.

    Create a new search filter

    You can create a new search filter by instantiating the AssetSearchFilter class, like so:

    
    var assetSearchFilter = new AssetSearchFilter(project);
    
    

    Searches are scoped to a specific organization and project. However, the instance can be reused to search for assets in different projects and organizations by updating the properties.

    • To update the search to another organization, you can use the Organization property, like so:
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Unity.Cloud.Assets.Documentation.Manual
    {
        public class UseCaseSearchAssetsExample
        {
            readonly IProject project;
    
            public UseCaseSearchAssetsExample(IProject project)
            {
                this.project = project;
            }
    
            public void Example(IProject newProject)
            {
    #region Example_Constructor
    
    var assetSearchFilter = new AssetSearchFilter(project);
    
    #endregion
    
    #region Example_Project
    
    assetSearchFilter.Project.Include(newProject);
    
    #endregion
    
    #region Example_NameInclude
    
    assetSearchFilter.Name.Include("my cool asset");
    
    #endregion
    
    #region Example_NameExclude
    
    assetSearchFilter.Name.Exclude("my mediocre asset");
    
    #endregion
    
    #region Example_NameAny
    
    assetSearchFilter.Name.ForAny("cool");
    
    #endregion
    
    #region Example_TagsInclude
    
    assetSearchFilter.Tags.Include("tag1", "tag2", "tag3");
    
    #endregion
    
            }
    
            IAsyncEnumerable<IAsset> SearchAsync(IAssetProvider assetProvider, IAssetSearchFilter assetSearchFilter)
            {
    #region Example_Search
    
    Pagination pagination = new Pagination(nameof(IAsset.Name), new Range(0, 10), Pagination.Order.Ascending);
    return assetProvider.SearchAsync(assetSearchFilter, pagination, CancellationToken.None);
    
    #endregion
            }
    
            async Task DisplayResultsIndividually(IAssetProvider assetProvider, IAssetSearchFilter assetSearchFilter)
            {
                var pagination = new Pagination(nameof(IAsset.Name), Range.All);
    
    #region Example_Foreach
    
    var assets = assetProvider.SearchAsync(assetSearchFilter, pagination, CancellationToken.None);
    await foreach (var asset in assets)
    {
        Console.WriteLine(asset.Name + " is available for use.");
    
        // Do something with each `asset` as it becomes available.
    }
    
    #endregion
            }
    
            async Task DisplayResults(IAssetProvider assetProvider, IAssetSearchFilter assetSearchFilter)
            {
                var pagination = new Pagination(nameof(IAsset.Name), Range.All);
    
    #region Example_ToList
    
    var assets = assetProvider.SearchAsync(assetSearchFilter, pagination, CancellationToken.None);
    
    var assetList = new List<IAsset>();
    await foreach (var asset in assets)
    {
        assetList.Add(asset);
    }
    
    // Do something with the complete `assetList`
    
    #endregion
            }
        }
    }
    
    • To update the search to another project, you can use the Project property, like so:
    
    assetSearchFilter.Project.Include(newProject);
    
    

    Each searchable property provides 3 avenues for searching:

    • Include - The property must match the value exactly.
    • Exclude - The property must not match the value.
    • Any - The property may contain the value. This represents an OR operation to be applied on all properties that include the Any value.

    To compute the search results, you can use the SearchAsync method of an IAssetProvider, like so:

    
    Pagination pagination = new Pagination(nameof(IAsset.Name), new Range(0, 10), Pagination.Order.Ascending);
    return assetProvider.SearchAsync(assetSearchFilter, pagination, CancellationToken.None);
    
    

    The Pagination struct is used to control the range of results to be returned and the ordering of results.

    In this example, the first 10 results displayed are sorted by the asset name in ascending order. The Search method returns an awaitable IAsyncEnumerable that will return each IAsset result.

    The results can be iterated over using a foreach loop and used as they become available, like so:

    
    var assets = assetProvider.SearchAsync(assetSearchFilter, pagination, CancellationToken.None);
    await foreach (var asset in assets)
    {
        Console.WriteLine(asset.Name + " is available for use.");
    
        // Do something with each `asset` as it becomes available.
    }
    
    

    Alternatively, the results can be iterated over and compiled into a list, so that the complete set of results can be used, like so:

    
    var assets = assetProvider.SearchAsync(assetSearchFilter, pagination, CancellationToken.None);
    
    var assetList = new List<IAsset>();
    await foreach (var asset in assets)
    {
        assetList.Add(asset);
    }
    
    // Do something with the complete `assetList`
    
    

    Search by Name

    • You can search for assets by name using the Name property of the AssetSearchFilter class, like so:
    
    assetSearchFilter.Name.Include("my cool asset");
    
    

    Note: This type of search checks for assets whose entire name exactly matches the parameter.

    • You can also exclude assets by name, like so:
    
    assetSearchFilter.Name.Exclude("my mediocre asset");
    
    
    • You can also search for assets whose name contains a specific string, like so:
    
    assetSearchFilter.Name.ForAny("cool");
    
    

    Search by Tags

    You can search for assets by tag using the Tags property of the AssetSearchFilter class, like so:

    
    assetSearchFilter.Tags.Include("tag1", "tag2", "tag3");
    
    

    Note: This type of search checks for assets whose tag list contains all the included parameters.

    Custom Search Filter

    You can also create a custom search filter by implementing the IAssetSearchFilter interface.

    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