docs.unity3d.com
    Show / Hide Table of Contents

    Querying metadata

    Setting up a IMetadataProvider

    IMetadataProvider is the first entry point for any metadata query. The constructor of the default implementation requires:

    • An instance of IServiceHttpClient and ServiceHostConfiguration. See Best practices: dependency injection for more information.
    • A sceneId and sceneVersion. See Unity Cloud Storage documentation for more information.

    Querying metadata

    With a IMetadataProvider, you can perform a query by combining the Query and the ExecuteAsync methods.

    Once the query has been executed, you can easily iterate through the matches which each hold:

    • OwnerId to point to the single Owner that contains metadata
    • RequestedMetadata to expose the queried metadata attached to this owner.

    Filtering

    The query described in the section above is the most standard query that you could expect, as it doesn't use any filter. Following a LINQ-like approach, the query system provides a way to build more advanced queries.

    SELECT filter, to query a subset of the metadata

    Using the Select method, you can specialize the query to ensure that only a specific set of root keys will be included in the response.

    SELECT-ALL filter, to query everything from the metadata

    Using the SelectAll method, the whole metadata content for every instances will be included in the response.

    SELECT-ONLY-ID filter, to query no metadata content

    Using the SelectOnlyId method, no metadata will be returned. The result will only contains the ids. This is especially useful when used with a Where filter.

    INCLUDED-IN filter, to query a subset of scene instances

    Using the IncludedIn method, you can specialize the query to ensure that only a specific subset of instances in the scene will be included in the response.

    WHERE filter, to apply a metadata-based condition

    Using the WhereKeyEquals method, you can specialize the query to ensure that the matches to be included in the response follow a specific metadata-based condition:

    • Metadata should contain a specific key.
    • This key should have a value exactly equal to a constant value.

    Combining filters

    You can use any combination of the filters described above to build advanced queries and apply multiple filters at once.

    Using the result

    MatchCollection

    Since MatchCollection is a ReadOnlyDictionnary, you can use your usual dictionaries method on it to get the data you want. For example, you can:

    • Use the property Keys to fetch all the contained OwnerId
    • Use the property Values to fetch all the contained MetadataObject
    • Use the square bracket operator to fetch a specific MetadataObject associated to an OwnerId

    MetadataObjects and MetadataValues

    The Metadata is always returned as a MetadataObject as it always is a valid JSON object at the root. MetadataObject extends MetadataContainer that is also extended by MetadataValue and MetadataArray. You can use a MetadataObject as a ReadOnlyDictionnary and access any valid JSON root objects, values or arrays by using the square bracket operator. When trying to read a value you can either use ToString() or ToNumber() depending on the underlying type.

    Helper methods

    Getting all keys in the scene

    By using the GetAllKeysAsync method, you can retrieve all the root keys that are present in the scene.

    Note: This method can have lower performance if the scene contains too much content.

    Getting all owner ids in the scene

    By using the GetAllIdsAsync method, you can retrieve all the owner ids present in the scene.

    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