Use case: Querying metadata from raycasting
To accomplish this use case, do the following:
- Upload an asset
- Create a cloud streaming behavior
- Set up a new scene
Before you start
Before you start, install the following Unity Cloud package dependency from the registry by name:
unity.cloud.identity
For more information on how to install packages by name, see Installing a package by name
How do I...?
Upload an asset
To upload an asset, follow these steps
Create a cloud streaming behavior
To create a cloud streaming behavior and set up a new scene, follow these steps
Set up the MetadataRepository
To set up the MetadataRepository
, refer to Set up a IMetadataRepository.
Use Data-Streaming to get an instance's metadata
Use the Data-Streamer's Raycaster
public async Task RaycastWithTheDataStreamer(IStage stage, Camera camera)
{
await stage.RaycastAsync((DoubleRay)camera.ScreenPointToRay(Input.mousePosition), camera.farClipPlane);
}
By passing a Ray
to the RaycastAsync
method, you get a RaycastResult
. A normal use-case would be to pass the mouse position converted into a ray.
Use the RaycastResult
public async Task UsingTheRaycastResult(IStage stage, Camera camera)
{
var raycastResult = await stage.RaycastAsync((DoubleRay)camera.ScreenPointToRay(Input.mousePosition), camera.farClipPlane);
if (string.IsNullOrEmpty(raycastResult.InstanceId.ToString()))
{
//no instance or no indexed texture was hit
}
}
The RaycastResult
contains a property named InstanceId
. The string is empty if there is no hit with the raycast. This can be caused by:
- The raycast hit nothing
- The dataset is too old and doesn't contain an indexed texture.
Use the InstanceId for a metadata query
public async Task<MetadataInstance> QueryingTheInstanceMetadata(IStage stage, Camera camera, IMetadataRepository metadataRepository)
{
var raycastResult = await stage.RaycastAsync((DoubleRay)camera.ScreenPointToRay(Input.mousePosition), camera.farClipPlane);
MetadataInstance result = default;
if (!string.IsNullOrEmpty(raycastResult.InstanceId.ToString()))
{
result = await metadataRepository
.Query()
.WhereInstanceEquals(raycastResult.InstanceId)
.GetFirstOrDefaultAsync(default);
}
return result;
}
The obtained InstanceId
can then be used directly into the metadata query builder's WhereInstanceEquals
method. For more info on how to build metadata queries, see Querying metadata
Use Metadata to filter
The Unity Cloud Metadata package can be used to get a list of InstanceId
that can then be provided to the DataStreamer to filter them out.