Last active
March 18, 2025 20:54
-
-
Save DanMillerDev/9d35f76c16abcdf4a4d8d7d375ac7c4f to your computer and use it in GitHub Desktop.
A script that places and manages Anchors, checking if the platform supports saved anchors and writing the ID to a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.XR.ARFoundation; | |
using UnityEngine.XR.ARFoundation.Samples; | |
public class AnchorPlacementManager : MonoBehaviour | |
{ | |
[SerializeField] | |
ARAnchorManager m_AnchorManager; | |
[SerializeField] | |
GameObject m_AnchorPrefab; | |
void Update() | |
{ | |
#if UNITY_EDITOR | |
// place anchor with middle of camera raycast | |
if (Mouse.current.leftButton.wasPressedThisFrame) | |
{ | |
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out RaycastHit hit)) | |
{ | |
PlaceAnchor(hit.point, Quaternion.LookRotation(hit.normal)); | |
} | |
} | |
#endif | |
} | |
async Awaitable SaveAnchorAsync(ARAnchor anchor) | |
{ | |
var result = await m_AnchorManager.TrySaveAnchorAsync(anchor); | |
if (result.status.IsError()) | |
{ | |
Debug.LogError($"Failed to save anchor {anchor.trackableId} with status {result.status}"); | |
return; | |
} | |
Debug.Log($"Successfully saved anchor {anchor.trackableId}"); | |
AnchorRecallManager.savedGuidsByTrackableId[anchor.trackableId] = result.value; | |
await AnchorSaveFile.instance.SaveAnchorIdAsync(result.value); | |
} | |
public async void RemoveAnchor(ARAnchor anchor) | |
{ | |
// Erase the persistent storage associated with the anchor | |
try | |
{ | |
if (m_AnchorManager.descriptor.supportsEraseAnchor) | |
await EraseAnchorAsync(anchor); | |
} | |
catch (OperationCanceledException) | |
{ | |
// Do nothing | |
} | |
// Stop tracking the anchor | |
var result = m_AnchorManager.TryRemoveAnchor(anchor); | |
if (result) | |
{ | |
Debug.Log(anchor.trackableId + " Anchor removed"); | |
} | |
} | |
async Awaitable EraseAnchorAsync(ARAnchor anchor) | |
{ | |
if (!AnchorRecallManager.savedGuidsByTrackableId.TryGetValue(anchor.trackableId, out var savedGuid)) | |
{ | |
Debug.LogError($"Failed to erase saved data for anchor: {anchor.trackableId}"); | |
} | |
else | |
{ | |
var result = await m_AnchorManager.TryEraseAnchorAsync(savedGuid); | |
if (result.IsSuccess()) | |
{ | |
Debug.Log($"Successfully erased anchor {anchor.trackableId}"); | |
await AnchorSaveFile.instance.EraseAnchorIdAsync(savedGuid); | |
AnchorRecallManager.savedGuidsByTrackableId.Remove(anchor.trackableId); | |
} | |
else | |
Debug.LogError($"Erase operation failed for anchor {anchor.trackableId}"); | |
} | |
} | |
public void RemoveAllAnchors() | |
{ | |
TrackableCollection<ARAnchor> currentSetOfAnchors = m_AnchorManager.trackables; | |
foreach(var anchor in currentSetOfAnchors) | |
{ | |
RemoveAnchor(anchor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment