text
stringlengths
0
1.11k
/*
* We respond to five button events:
*
* Left trigger: Create a saveable (green) anchor.
* Right trigger: Create a non-saveable (red) anchor.
* A: Load, Save and display all saved anchors (green only)
* X: Destroy all runtime anchors (red and green)
* Y: Erase all anchors (green only)
* others: no action
*/
void Update()
{
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger))
{
//create a green (saveable) spatial anchor
GameObject gs = PlaceAnchor(_saveableAnchorPrefab, _saveableTransform.position, _saveableTransform.rotation); //anchor A
_workingAnchor = gs.AddComponent<OVRSpatialAnchor>();
CreateAnchor(_workingAnchor, true);
}
else if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger))
{
//create a red (non-saveable) spatial anchor.
GameObject gs = PlaceAnchor(_nonSaveableAnchorPrefab, _nonSaveableTransform.position, _nonSaveableTransform.rotation); //anchor b
_workingAnchor = gs.AddComponent<OVRSpatialAnchor>();
CreateAnchor(_workingAnchor, false);
}
else if (OVRInput.GetDown(OVRInput.Button.One))
{
LoadAllAnchors(); // load saved anchors
}
else 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);
//Debug.Log("Destroyed an Anchor " + spAnchor.Uuid.ToString() + "!");
}
}
//clear the list of running anchors
_allRunningAnchors.Clear();
}
else if (OVRInput.GetDown(OVRInput.Button.Four))
{
EraseAllAnchors(); // erase all saved (green) anchors
}
else // any other button?
{
// no other actions tracked
}
}
/****************************** Button Handlers ***********************/
/******************* Create Anchor Methods *****************/
public void CreateAnchor(OVRSpatialAnchor spAnchor, bool saveAnchor)
{
//use a Unity coroutine to manage the async save
StartCoroutine(anchorCreated(_workingAnchor, saveAnchor));
}
/*
* Unity Coroutine
* We need to make sure the anchor is ready to use before we save it.
* Also, only save if specified
*/
public IEnumerator anchorCreated(OVRSpatialAnchor osAnchor, bool saveAnchor)
{
// keep checking for a valid and localized anchor state
while (!osAnchor.Created && !osAnchor.Localized)
{
yield return new WaitForEndOfFrame();
}
//Save the anchor to a local List so we can track during the current session
_allRunningAnchors.Add(osAnchor);
// we save the saveable (green) anchors only
if (saveAnchor)
{
//when ready, save the anchor.
osAnchor.Save((anchor, success) =>
{
if (success)
{
//save UUID to external storage so we can refer to the anchors in a later session
SaveAnchorUuidToExternalStore(anchor);
//Debug.Log("Anchor " + osAnchor.Uuid.ToString() + " Saved!");
//keep tabs on anchors in local storage
_allSavedAnchors.Add(anchor);