示例#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));
    }
示例#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;
                cloudid            = result.Anchor.CloudId;
                submitlock         = true;
                _hostedCloudAnchor = new CloudAnchorHistory(cloudid, cloudid);
                Debug.LogFormat("Cloud Anchor Name: {0}", _hostedCloudAnchor.Id);
                OnAnchorHostedFinished(true, result.Anchor.CloudId);
                //todo, work on this part
                //load available prefrab from Resource/Prefab
                //Do Dropdown List, allow user to select the prefab.
                //then press confirm(Button) to spawn prefab at the hitpose.

                //Store all prefab from "Resources/Prefab" in the array
                prefabList = Resources.LoadAll <GameObject>("Prefab");
                if (prefabList == null)
                {
                    Debug.Log("prefab List is null, Resources.LoadAll failed");
                }
                //get the index of prefab selected by user from the dropdown menu
                //might be because of null exception, since nothing is chosen yet.
                int prefabSelectedIndex = prefabDropdown.GetComponent <Dropdown>().value;

                //Get the prefab from folder "Resources/Prefab/{Name of object selected}"
                prefabToPlace = Resources.Load("Prefab/" + prefabList[prefabSelectedIndex].name) as GameObject;


                gameRef = Instantiate(prefabToPlace, result.Anchor.transform);
                prefabsOnMap.Add(gameRef);

                int typeObj = 0;     // check if this is the correct type
                if (typeObj == 0)
                {
                    Show();
                }
                else
                {
                    networker.AddCloudID(cloudid, "", 1, objectType);
                };

                currentCloudTransform = result.Anchor.transform;
                prefabToPlace         = Resources.Load("Prefab/" + prefabList[prefabDropdown.GetComponent <Dropdown>().value].name) as GameObject;
                if (gameRef)
                {
                    Destroy(gameRef);
                }
                gameRef = Instantiate(prefabToPlace, currentCloudTransform);

                Show();
            }
        });
    }