コード例 #1
0
    private void CloudManager_AnchorLocated(object sender, Microsoft.Azure.SpatialAnchors.AnchorLocatedEventArgs args)
    {
        Log(nameof(CloudManager_AnchorLocated));

        //Forward the event to the FinAnchor section
        ReportAnchorLocationResultReceived(args);
    }
コード例 #2
0
    /// <summary>
    /// The watcher created in <see cref="FindSpatialAnchor(string)"/> invokes this event for every anchor requested
    /// This event fires if an anchor is located or also if it cannot be located.
    /// </summary>
    /// <param name="args"></param>
    private void ReportAnchorLocationResultReceived(Microsoft.Azure.SpatialAnchors.AnchorLocatedEventArgs args)
    {
        if (args.Watcher != null)
        {
            args.Watcher.Stop();
        }
        switch (args.Status)
        {
        case LocateAnchorStatus.AlreadyTracked:
            Log($"The requested anchor {args.Identifier} was already tracked.", true);
            SendUpdateOnMainThread(AsaStatusEventType.FindAnchor_AlreadyTracked, status);
            break;

        case LocateAnchorStatus.Located:
            CloudSpatialAnchor foundAnchor = args.Anchor;

            Log($"The requested anchor {args.Identifier} was found!", true);

            if (foundAnchor == null)
            {
                Log($"Found the requested anchor but the returned anchor is null!", true);
                return;
            }

            SendUpdateOnMainThread(AsaStatusEventType.FindAnchor_Finished, status);

            ExecuteOnMainThread(() =>
            {
                Log("Processing the found anchor's position", true);
                // Notify AnchorFeedbackScript
                Pose anchorPose = Pose.identity;

#if UNITY_ANDROID || UNITY_IOS
                anchorPose = foundAnchor.GetPose();
#endif

#if WINDOWS_UWP || UNITY_WSA
                // HoloLens: The position will be set based on the unityARUserAnchor that was located.

                // Create a local anchor at the location of the object in question
                gameObject.CreateNativeAnchor();

                // On HoloLens, if we do not have a cloudAnchor already, we will have already positioned the
                // object based on the passed in worldPos/worldRot and attached a new world anchor,
                // so we are ready to commit the anchor to the cloud if requested.
                // If we do have a cloudAnchor, we will use it's pointer to setup the world anchor,
                // which will position the object automatically.
                if (foundAnchor != null)
                {
                    Log("Local anchor position successfully set to Azure anchor position", true);

                    var worldAnchor = gameObject.GetComponent <UnityEngine.XR.WSA.WorldAnchor>();
                    if (worldAnchor != null)
                    {
                        worldAnchor.SetNativeSpatialAnchorPtr(foundAnchor.LocalAnchor);
                    }
                    else
                    {
                        Log("WorldAnchorComponent was null", true);
                    }
                }
#else
                Log($"Setting object to anchor pose with position '{anchorPose.position}' and rotation '{anchorPose.rotation}'", true);
                transform.position = anchorPose.position;
                transform.rotation = anchorPose.rotation;

                // Create a native anchor at the location of the object in question
                //gameObject.CreateNativeAnchor();
#endif
            });

            break;

        case LocateAnchorStatus.NotLocatedAnchorDoesNotExist:
            // The anchor was deleted or never existed in the first place
            // Drop it, or show UI to ask user to anchor the content anew
            Log($"The requested anchor {args.Identifier} does not exist.", true);
            SendUpdateOnMainThread(AsaStatusEventType.FindAnchor_DoesNotExist, status);
            break;

        case LocateAnchorStatus.NotLocated:
            // The anchor hasn't been found given the location data
            // The user might in the wrong location, or maybe more data will help
            // Show UI to tell user to keep looking around
            Log($"The requested anchor {args.Identifier} could not be located.", true);
            SendUpdateOnMainThread(AsaStatusEventType.FindAnchor_CouldNotLocate, status);
            break;
        }
    }