public async Task Initialize() { await GetComponent <SpatialAnchorManager>().CreateSessionAsync(); feedback.text = "Created Session"; await GetComponent <SpatialAnchorManager>().StartSessionAsync(); feedback.text = "Started Session"; await SaveCurrentObjectAnchorToCloudAsync(); long anchorNumber = -1; anchorNumber = (await anchorExchanger.StoreAnchorKey(currentCloudAnchor.Identifier)); GetComponentInChildren <TextMeshPro>().text = anchorNumber.ToString(); GetComponent <SpatialAnchorManager>().StopSession(); feedback.text = "Stopped Session"; }
public void HandleTap(TappedEventArgs tapEvent) { // Construct a Ray using forward direction of the HoloLens. Ray HeadRay = new Ray(tapEvent.headPose.position, tapEvent.headPose.forward); Vector3 PointInAir = HeadRay.GetPoint(1.5f); // Spawn Object GameObject protoCopy = Instantiate(protoObject); protoCopy.transform.position = PointInAir; float rotX = UnityEngine.Random.Range(10, 70); float rotY = UnityEngine.Random.Range(10, 70); protoCopy.transform.rotation = Quaternion.Euler(rotX, 0, rotY); // Empty Object to Hold Anchor GameObject anchorCopy = Instantiate(anchorProto); anchorCopy.transform.position = PointInAir; // Bind anchor holder and the spawned object protoCopy.transform.SetParent(anchorCopy.transform); anchorCopy.AddComponent <WorldAnchor>(); CloudSpatialAnchor localCloudAnchor = new CloudSpatialAnchor(); localCloudAnchor.LocalAnchor = anchorCopy.GetComponent <WorldAnchor>().GetNativeSpatialAnchorPtr(); localCloudAnchor = StoreAppPropertiesUsingTransform(protoCopy.transform, localCloudAnchor, 0); localCloudAnchor.Expiration = DateTimeOffset.Now.AddDays(2); Debug.Log(CloudManager.EnoughDataToCreate); currentCloudAnchor = null; Task.Run(async() => { QueueOnUpdate(() => { Debug.Log("In Task......."); anchorCopy.GetComponent <MeshRenderer>().material.color = anchorColorYellow; }); while (!CloudManager.EnoughDataToCreate) { await Task.Delay(300); float createProgress = CloudManager.GetSessionStatusIndicator(AnchorWrapper.SessionStatusIndicatorType.RecommendedForCreate); QueueOnUpdate(new Action(() => Debug.Log(createProgress))); } bool success = false; try { QueueOnUpdate(new Action(() => Debug.Log("Saving..."))); notInUpdateTask = false; currentCloudAnchor = await CloudManager.StoreAnchorInCloud(localCloudAnchor); success = currentCloudAnchor != null; long anchorNumber = -1; if (success) { anchorNumber = (await anchorExchanger.StoreAnchorKey(currentCloudAnchor.Identifier)); notInUpdateTask = true; QueueOnUpdate(() => { Debug.Log("SaveSuccess!"); // Store in Local GameObject Dictionary localAnchorGameObjects.Add(currentCloudAnchor.Identifier, anchorCopy); localGameObjectsCloudAnchor.Add(anchorCopy.GetInstanceID().ToString(), currentCloudAnchor); Debug.Log(currentCloudAnchor.Identifier); Debug.Log("anchorNumber: " + anchorNumber.ToString()); anchorCopy.GetComponent <MeshRenderer>().material.color = anchorColorPink; localAnchorSaved.Add(anchorCopy.GetInstanceID().ToString(), true); }); } else { QueueOnUpdate(() => { Debug.Log("SaveFailed!"); localAnchorSaved.Add(anchorCopy.GetInstanceID().ToString(), false); anchorCopy.GetComponent <MeshRenderer>().material.color = anchorColorPurple; }); } } catch (Exception ex) { QueueOnUpdate(new Action(() => Debug.LogException(ex))); } }); }