Use case: Integrating with Unity Cloud Data Streaming
How do I...?
Set up the MetadataRepository
To set up the MetadataRepository, refer to Set up a IMetadataRepository.
Set up the DataStreamer
Refer to Getting Started with Data Streaming for more information.
Use Datastreaming to get an instance's metadata
Use the Datastreamer'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.