private static async Task <WorldAnchorCoordinateService> InitializeSharedCoordinateServiceAsync()
        {
            WorldAnchorCoordinateService service = new WorldAnchorCoordinateService();
            await service.InitializeKnownCoordinatesAsync();

            return(service);
        }
            public async Task <ISpatialCoordinate> LocalizeAsync(CancellationToken cancellationToken)
            {
                WorldAnchorCoordinateService coordinateService = await localizer.coordinateServiceTask.Unless(cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }

                if (settings.Mode == WorldAnchorLocalizationMode.LocateExistingAnchor)
                {
                    if (coordinateService.TryGetKnownCoordinate(settings.AnchorId, out ISpatialCoordinate coordinate))
                    {
                        return(coordinate);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(await coordinateService.CreateCoordinateAsync(settings.AnchorId, settings.AnchorPosition, settings.AnchorRotation, cancellationToken));
                }
            }
        protected override void Start()
        {
            base.Start();

            // Initialize the shared coordinate service during Start. This way, WorldAnchors
            // can be located and tracking even before a connection is formed to the device.
            // This will reduce the amount of time needed to located anchors once a client
            // does connect to the device.
            coordinateServiceTask = WorldAnchorCoordinateService.GetSharedCoordinateServiceAsync();
        }
 private async Task <ISpatialCoordinate> GetOrCreateCoordinate(WorldAnchorCoordinateService coordinateService, CancellationToken cancellationToken)
 {
     if (settings.Mode == WorldAnchorLocalizationMode.LocateExistingAnchor)
     {
         if (coordinateService.TryGetKnownCoordinate(settings.AnchorId, out ISpatialCoordinate coordinate))
         {
             return(coordinate);
         }
         else
         {
             return(null);
         }
     }
     else
     {
         return(await coordinateService.CreateCoordinateAsync(settings.AnchorId, settings.AnchorPosition, settings.AnchorRotation, cancellationToken));
     }
 }
 private async Task <ISpatialCoordinate> GetOrCreateCoordinateEditor(WorldAnchorCoordinateService coordinateService, CancellationToken cancellationToken)
 {
     if (settings.Mode == WorldAnchorLocalizationMode.LocateExistingAnchor)
     {
         if (HasVectorProperty($"{nameof(WorldAnchorSpatialLocalizer)}_{settings.AnchorId}_position") && HasVectorProperty($"{nameof(WorldAnchorSpatialLocalizer)}_{settings.AnchorId}_rotation"))
         {
             return(await coordinateService.CreateCoordinateAsync(settings.AnchorId, GetVectorProperty($"{nameof(WorldAnchorSpatialLocalizer)}_{settings.AnchorId}_position"), Quaternion.Euler(GetVectorProperty($"{nameof(WorldAnchorSpatialLocalizer)}_{settings.AnchorId}_rotation")), cancellationToken));
         }
         else
         {
             return(null);
         }
     }
     else
     {
         SetVectorProperty($"{nameof(WorldAnchorSpatialLocalizer)}_{settings.AnchorId}_position", settings.AnchorPosition);
         SetVectorProperty($"{nameof(WorldAnchorSpatialLocalizer)}_{settings.AnchorId}_rotation", settings.AnchorRotation.eulerAngles);
         return(await coordinateService.CreateCoordinateAsync(settings.AnchorId, settings.AnchorPosition, settings.AnchorRotation, cancellationToken));
     }
 }
            /// <inheritdoc />
            public override async Task <ISpatialCoordinate> LocalizeAsync(CancellationToken cancellationToken)
            {
                if (!defaultCancellationToken.CanBeCanceled)
                {
                    Debug.LogError("Session is invalid. No localization performed.");
                    return(null);
                }

                ISpatialCoordinate spatialCoordinate = null;

                using (var cancellableCTS = CancellationTokenSource.CreateLinkedTokenSource(defaultCancellationToken, cancellationToken))
                {
                    WorldAnchorCoordinateService coordinateService = await localizer.coordinateServiceTask.Unless(cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }

                    if (settings.Mode == WorldAnchorLocalizationMode.LocateExistingAnchor)
                    {
                        if (coordinateService.TryGetKnownCoordinate(settings.AnchorId, out ISpatialCoordinate coordinate))
                        {
                            return(coordinate);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        spatialCoordinate = await coordinateService.CreateCoordinateAsync(settings.AnchorId, settings.AnchorPosition, settings.AnchorRotation, cancellationToken);
                    }
                }

                return(await Task.FromResult(spatialCoordinate));
            }
            /// <inheritdoc />
            public override async Task <ISpatialCoordinate> LocalizeAsync(CancellationToken cancellationToken)
            {
                if (!defaultCancellationToken.CanBeCanceled)
                {
                    Debug.LogError("Session is invalid. No localization performed.");
                    return(null);
                }

                using (var cancellableCTS = CancellationTokenSource.CreateLinkedTokenSource(defaultCancellationToken, cancellationToken))
                {
                    WorldAnchorCoordinateService coordinateService = await localizer.coordinateServiceTask.Unless(cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }

#if UNITY_EDITOR
                    return(await GetOrCreateCoordinateEditor(coordinateService, cancellationToken));
#else
                    return(await GetOrCreateCoordinate(coordinateService, cancellationToken));
#endif
                }
            }