using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SpatialAnchorManager : MonoBehaviour { private ApplicationManager _applicationManager; // This reusable buffer helps reduce pressure on the garbage collector private readonly List _unboundAnchors = new(); private const string NumUuidsPlayerPref = "NumUuids"; private void Awake() { _applicationManager = GetComponent(); if (_applicationManager == null) { Debug.LogError("[DCDC] ApplicationManager component not found on the GameObject."); } } private void Start() { _applicationManager.SetInProgress(true); LoadSavedAnchors(); } private void Update() { // spatial anchor options are only available to the host if (_applicationManager.InProgress || _applicationManager.IsHost == false) return; if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.RTouch)) { CreateSpatialAnchor(); } if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick, OVRInput.Controller.RTouch)) { UnsaveAllAnchors(); } } private async void LoadSavedAnchors() { try { if (!PlayerPrefs.HasKey(NumUuidsPlayerPref)) PlayerPrefs.SetInt(NumUuidsPlayerPref, 0); var playerUuidCount = PlayerPrefs.GetInt(NumUuidsPlayerPref); if (playerUuidCount == 0) { _applicationManager.SetInProgress(false); return; } Debug.Log($"[DCDC] Loading {playerUuidCount} anchors from PlayerPrefs."); var uuids = new Guid[playerUuidCount]; for (var i = 0; i < playerUuidCount; i++) { var uuidKey = "uuid" + i; var currentUuid = PlayerPrefs.GetString(uuidKey); uuids[i] = new Guid(currentUuid); } // Load var result = await OVRSpatialAnchor.LoadUnboundAnchorsAsync(uuids, _unboundAnchors); if (result.Success) { Debug.Log("[DCDC] Anchors loaded successfully."); // Note result.Value is the same as _unboundAnchors foreach (var unboundAnchor in result.Value) { unboundAnchor.LocalizeAsync().ContinueWith((success, _) => { if (success) { unboundAnchor.TryGetPose(out var pose); var spatialAnchor = Instantiate(_applicationManager.hostPlaceholderMarker, pose.position, pose.rotation); var button = spatialAnchor.gameObject.GetComponentInChildren