text
stringlengths
0
1.11k
return Instantiate(prefab, p, r);
}
The Unity Instantiate() method will display and orient the anchor prefab in the VR environment.
Track and Save a Green or Red Capsule
Because saving to local storage is an asynchronous operation, we will use a Unity coroutine to make sure the capsule exists before we try to save it. For convenience, we use the same methods for both green and red capsules. The call to CreateAnchor() starts both accounting and saving.
public void CreateAnchor(OVRSpatialAnchor spAnchor, bool saveAnchor)
{
StartCoroutine(anchorCreated(_workingAnchor, saveAnchor)); //use a coroutine to manage the async save
}
The main work is done within the coroutine. We yield until the anchor is available. Then if saveAnchor is true, we attempt to save the anchor. The Save() method defaults to local storage (OVRSpace.StorageLocation.Local), so we don’t have to specify it here.
public IEnumerator anchorCreated(OVRSpatialAnchor osAnchor, bool saveAnchor)
{
while (!osAnchor.Created && !osAnchor.Localized)
{
yield return new WaitForEndOfFrame(); //keep checking
}
//Save the anchor to a local List so we can refer to it
_allRunningAnchors.Add(osAnchor);
if (saveAnchor) // we save the saveable (green) anchors only
{
osAnchor.Save((anchor, success) =>
{
if (success)
{
//keep tabs on anchors in local storage
_allSavedAnchors.Add(anchor);
//if we wanted to save UUID to external storage so
// we could refer to it in a future session, we
// would do so here also
}
});
}
}
We want to make sure we can account for the capsules we create, so we’ll use it, so after the anchor is created, we add it to the list of known saved anchors.
Destroy Displayed Anchors
After we have pressed the two index triggers a few times, _allSavedAnchors contains the green ones, and _allRunningAnchors contains both red and green. In this tutorial we destroy all green and red capsules from the current scene by pressing the X button. This action is immediate, not asynchronous.
if (OVRInput.GetDown(OVRInput.Button.Three)) //x button
{
//Destroy all anchors from the scene, but don't erase them from storage
using (var enumerator = _allRunningAnchors.GetEnumerator())
{
while (enumerator.MoveNext())
{
var spAnchor = enumerator.Current;
Destroy(spAnchor.gameObject);
}
}
//clear the list of running anchors
_allRunningAnchors.Clear();
}
Though we destroy all the capsules from the scene, any saveable green capsules we have already saved are still in local storage.
Create a Method to Load Anchors
We will use the A button to load any anchors saved to local storage:
if (OVRInput.GetDown(OVRInput.Button.One))
{
LoadAllAnchors(); // load saved anchors
}
As we describe in Spatial Anchors Overview, loading from either local or cloud storage is a three-step process:
Load a spatial anchor from storage using its UUID. At this point it is unbound.
Localize each unbound spatial anchor to fix it in its intended virtual location.
Bind each spatial anchor to an OVRSpatialAnchor().
Load and Localize Anchors
We load and localize each anchor with one method. First, we set the LoadOptions struct with both the UUIDs we want to load, and the storage location to load them from. Then we call OVRSpatialAnchor.LoadUnboundAnchors(), and iterate over the result.
public void LoadAllAnchors()
{
OVRSpatialAnchor.LoadOptions options = new OVRSpatialAnchor.LoadOptions
{
Timeout = 0,
StorageLocation = OVRSpace.StorageLocation.Local,
Uuids = GetSavedAnchorUUIDs()
};
OVRSpatialAnchor.LoadUnboundAnchors(options, _anchorSavedUUIDList =>
{
if (_anchorSavedUUIDList == null)
{
return;
}
foreach (var anchor in _anchorSavedUUIDList)
{
if (anchor.Localized)
{
_onLoadAnchor(anchor, true);
}
else if (!anchor.Localizing)
{
anchor.Localize(_onLoadAnchor);
}