Unity Trackables
Updated: Nov 22, 2024
A “trackable” is something in the physical environment that can be detected and tracked in real-time, like a physical keyboard. Trackables are similar to
Scene Anchors, but they can be detected at runtime instead of being statically generated during Space Setup.
In order for your app to be able to detect trackables, you must configure an anchor tracker. You can then periodically query for trackables.
Trackables in MR Utility Kit
This topic describes the low-level API available in the Meta XR Core SDK. Use this if you want full control. For most use cases, consider the high-level API in the Mixed Reality Utility Kit package. To track a particular type of trackable, create and configure an OVRAnchor.Tracker
:
OVRAnchor.Tracker _tracker;
async void Start()
{
// Create a tracker
_tracker = new OVRAnchor.Tracker();
// Configure it to detect keyboards
var result = await _tracker.ConfigureAsync(new OVRAnchor.TrackerConfiguration
{
KeyboardTrackingEnabled = true,
});
if (result.Success)
{
// Keyboard tracking enabled!
}
}
If the requested configuration is not supported, or if the configuration cannot be fully satisfied at the current time, then
ConfigureAsync
will return a result indicating an error. Additionally, the
Configuration property represents the current state of the tracker. If
requestedConfiguration != tracker.Configuration
, some aspect of the requested configuration could not be satisifed.
The TrackerConfiguration
has static methods to check for feature support prior to configuring the tracker. For example, to check whether keyboard tracking is supported, use OVRAnchor.TrackerConfiguration.KeyboardTrackingSupported
.
List<OVRAnchor> _anchors = new();
async void UpdateTrackables()
{
var result = await _tracker.FetchTrackablesAsync(_anchors);
if (result.Success)
{
// ...
}
}
The resulting list of anchors should contain all anchors currently being tracked by the OVRAnchor.Tracker
. You can determine the type of anchor using OVRAnchor.GetTrackableType()
:
async void UpdateTrackables()
{
var result = await _tracker.FetchTrackablesAsync(_anchors);
if (result.Success)
{
foreach (var anchor in _anchors)
{
Debug.Log($"Anchor {anchor} a trackable of type {anchor.GetTrackableType()}");
}
}
}
The list of trackables can change over time, for example, as objects come in and out of view.
Typically, you should refresh the list of trackables periodically to check for new or removed trackables:
async void UpdateTrackables()
{
while (enabled)
{
var result = await _tracker.FetchTrackablesAsync(_anchors);
if (result.Success)
{
foreach (var anchor in _anchors)
{
Debug.Log($"Anchor {anchor} a trackable of type {anchor.GetTrackableType()}");
}
}
// Wait one second, then query again
await Task.Delay(TimeSpan.FromSeconds(1));
}
}