public void ToggleAnchorPersistence(ARAnchor anchor) { if (m_anchorStore == null) { Debug.Log($"Anchor Store was not available."); return; } SampleAnchor sampleAnchor = anchor.GetComponent <SampleAnchor>(); if (!sampleAnchor.Persisted) { // For the purposes of this sample, randomly generate a name for the saved anchor. string newName = $"anchor/{Guid.NewGuid().ToString().Substring(0, 4)}"; bool succeeded = m_anchorStore.TryPersistAnchor(anchor.trackableId, newName); if (!succeeded) { Debug.Log($"Anchor could not be persisted: {anchor.trackableId}"); return; } ChangeAnchorVisual(anchor, newName, true); } else { m_anchorStore.UnpersistAnchor(sampleAnchor.Name); ChangeAnchorVisual(anchor, "", false); } }
/// <summary> /// Steps to show problem with XRAnchorStore.Clear() /// </summary> /// <param name="anchorStore">An empty XRAnchorStore</param> /// <param name="anchorTrackableId">TrackableId for an ARAnchor</param> private static void TestAnchorStore(XRAnchorStore anchorStore, TrackableId anchorTrackableId) { string anchorName = "Anchor0"; // Add the anchor to the anchorStore. Since anchorStore is empty, this succeeds. anchorStore.TryPersistAnchor(anchorTrackableId, anchorName); // Add the anchor again. Since it is already in the anchorStore, this fails with error message emitted. // This is correct and expected. anchorStore.TryPersistAnchor(anchorTrackableId, anchorName); // [XR] [Mixed Reality OpenXR Anchor]: Could not persist anchor "Anchor0"; name already used in anchor store. // Clear the anchorStore. anchorStore.Clear(); // It seems to have cleared everything, because the anchorStore.PersistedAnchorNames.Count == 0 now. // But attempting to persist Anchor0 again will still result in the same error. // INCORRECT HERE. anchorStore.TryPersistAnchor(anchorTrackableId, anchorName); // [XR] [Mixed Reality OpenXR Anchor]: Could not persist anchor "Anchor0"; name already used in anchor store. // Now Un-persist the anchor. anchorStore.UnpersistAnchor(anchorName); // Persisting it now seems to succeed (or at least generates no error message.) anchorStore.TryPersistAnchor(anchorTrackableId, anchorName); }