Example #1
0
        /// <summary>
        /// Save the persistent Cloud Anchors history to local storage,
        /// also remove the oldest data if current storage has met maximal capacity.
        /// </summary>
        /// <param name="data">The Cloud Anchor history data needs to be stored.</param>
        public void SaveCloudAnchorHistory(CloudAnchorHistory data)
        {
            var history = LoadCloudAnchorHistory();

            // Sort the data from latest record to oldest record which affects the option order in
            // multiselection dropdown.
            history.Collection.Add(data);
            history.Collection.Sort((left, right) => right.CreatedTime.CompareTo(left.CreatedTime));

            // Remove the oldest data if the capacity exceeds storage limit.
            if (history.Collection.Count > _storageLimit)
            {
                history.Collection.RemoveRange(
                    _storageLimit, history.Collection.Count - _storageLimit);
            }

            PlayerPrefs.SetString(_persistentCloudAnchorsStorageKey, JsonUtility.ToJson(history));
        }
Example #2
0
        private void HostingCloudAnchor()
        {
            // There is no anchor for hosting.
            if (_anchorComponent == null)
            {
                return;
            }

            // There is a pending hosting task.
            if (_isHosting)
            {
                return;
            }

            // Hosting instructions:
            var cameraDist = (_qualityIndicator.transform.position -
                              Controller.MainCamera.transform.position).magnitude;

            if (cameraDist < _qualityIndicator.Radius * 1.5f)
            {
                InstructionText.text = "You are too close, move backward.";
                return;
            }
            else if (cameraDist > 10.0f)
            {
                InstructionText.text = "You are too far, come closer.";
                return;
            }
            else if (_qualityIndicator.ReachTopviewAngle)
            {
                InstructionText.text =
                    "You are looking from the top view, move around from all sides.";
                return;
            }
            else if (!_qualityIndicator.ReachQualityThreshold)
            {
                InstructionText.text = "Save the object here by capturing it from all sides.";
                // Can pass in ANY valid camera pose to the mapping quality API.
                // Ideally, the pose should represent users’ expected perspectives.
                DebugText.text = "Current mapping quality: " +
                                 XPSession.EstimateFeatureMapQualityForHosting(GetCameraPose());
                return;
            }

            // Start hosting:
            _isHosting           = true;
            InstructionText.text = "Processing...";
            DebugText.text       = "Mapping quality has reached sufficient threshold, " +
                                   "creating Cloud Anchor.";
            DebugText.text = string.Format(
                "FeatureMapQuality has reached {0}, triggering CreateCloudAnchor.",
                XPSession.EstimateFeatureMapQualityForHosting(GetCameraPose()));

#if ARCORE_IOS_SUPPORT
            var anchor = (UnityARUserAnchorComponent)_anchorComponent;
#else
            var anchor = (Anchor)_anchorComponent;
#endif

            // Creating a Cloud Anchor with lifetime = 1 day.
            // This is configurable up to 365 days when keyless authentication is used.
            XPSession.CreateCloudAnchor(anchor, 1).ThenAction(result =>
            {
                if (!_isHosting)
                {
                    // This is the pending task from previous session.
                    return;
                }

                if (result.Response != CloudServiceResponse.Success)
                {
                    Debug.LogFormat("Failed to host cloud anchor: {0}", result.Response);
                    OnAnchorHostedFinished(false, result.Response.ToString());
                }
                else
                {
                    Debug.LogFormat("Succeed to host cloud anchor: {0}", result.Anchor.CloudId);
                    int count          = Controller.LoadCloudAnchorHistory().Collection.Count;
                    _hostedCloudAnchor =
                        new CloudAnchorHistory("CloudAnchor" + count, result.Anchor.CloudId);
                    OnAnchorHostedFinished(true, result.Anchor.CloudId);
                }
            });
        }