Ejemplo n.º 1
0
        /// <summary>
        /// Executes the anchor operations from the localAnchorOperations queue.
        /// </summary>
        /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
        private void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
        {
            if (AnchorStore == null)
            {
                Debug.LogError("[WorldAnchorManager] Remove anchor called before anchor store is ready.");
                return;
            }

            string     anchorId           = anchorAttachmentInfo.AnchorName;
            GameObject anchoredGameObject = anchorAttachmentInfo.AnchoredGameObject;

            switch (anchorAttachmentInfo.Operation)
            {
            case AnchorOperation.Save:
                DoSaveAnchorOperation(anchorId, anchoredGameObject);
                break;

            case AnchorOperation.Delete:
                DoDeleteAnchorOperation(anchorId, anchoredGameObject);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function that actually adds the anchor to the game object.
        /// </summary>
        /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
        private void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
        {
            switch (anchorAttachmentInfo.Operation)
            {
            case AnchorOperation.Create:
                string     anchorName         = anchorAttachmentInfo.AnchorName;
                GameObject gameObjectToAnchor = anchorAttachmentInfo.GameObjectToAnchor;

                if (gameObjectToAnchor == null)
                {
                    Debug.LogError("GameObject must have been destroyed before we got a chance to anchor it.");
                    break;
                }

                WorldAnchor existingAnchor = gameObjectToAnchor.GetComponent <WorldAnchor>();
                if (existingAnchor != null)
                {
                    DestroyImmediate(existingAnchor);
                }

                // Try to load a previously saved world anchor.
                WorldAnchor savedAnchor = AnchorStore.Load(anchorName, gameObjectToAnchor);

                if (savedAnchor == null)
                {
                    // Either world anchor was not saved / does not exist or has a different name.
                    Debug.LogWarning(gameObjectToAnchor.name + " : World anchor could not be loaded for this game object. Creating a new anchor.");

                    // Create anchor since one does not exist.
                    CreateAnchor(gameObjectToAnchor, anchorName);
                }
                else
                {
                    savedAnchor.name = anchorName;
                    Debug.Log(gameObjectToAnchor.name + " : World anchor loaded from anchor store and updated for this game object.");
                }
                break;

            case AnchorOperation.Delete:
                if (AnchorStore == null)
                {
                    Debug.LogError("Remove anchor called before anchor store is ready.");
                    break;
                }

                GameObject gameObjectToUnanchor = anchorAttachmentInfo.GameObjectToAnchor;
                var        anchor = gameObjectToUnanchor.GetComponent <WorldAnchor>();

                if (anchor != null)
                {
                    AnchorStore.Delete(anchor.name);
                    DestroyImmediate(anchor);
                }
                else
                {
                    Debug.LogError("Cannot get anchor while deleting");
                }
                break;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Function that actually adds the anchor to the game object.
        /// </summary>
        /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
        private void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
        {
            string     AnchorName         = anchorAttachmentInfo.AnchorName;
            GameObject gameObjectToAnchor = anchorAttachmentInfo.GameObjectToAnchor;

            if (gameObjectToAnchor == null)
            {
                Debug.Log("GameObject must have been destroyed before we got a chance to anchor it.");
                return;
            }

            // Try to load a previously saved world anchor.
            WorldAnchor savedAnchor = AnchorStore.Load(AnchorName, gameObjectToAnchor);

            if (savedAnchor == null)
            {
                // Either world anchor was not saved / does not exist or has a different name.
                Debug.Log(gameObjectToAnchor.name + " : World anchor could not be loaded for this game object. Creating a new anchor.");

                // Create anchor since one does not exist.
                CreateAnchor(gameObjectToAnchor, AnchorName);
            }
            else
            {
                savedAnchor.name = AnchorName;
                Debug.Log(gameObjectToAnchor.name + " : World anchor loaded from anchor store and updated for this game object.");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes the anchor operations from the localAnchorOperations queue.
        /// </summary>
        /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
        private void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
        {
            GameObject anchoredGameObject = anchorAttachmentInfo.AnchoredGameObject;


            if (AnchorStore == null || anchoredGameObject == null || Application.isEditor)//no Wa if in editor
            {
                return;
            }



            switch (anchorAttachmentInfo.Operation)
            {
            case AnchorOperation.Save:

                //before we save, we delete the old anchor from the store
                if (new List <string>(AnchorStore.GetAllIds()).Contains(anchoredGameObject.name))
                {
                    AnchorStore.Delete(anchoredGameObject.name);
                }

                //gets the anchor to save from the GO, creates one, if there is none
                WorldAnchor anchorToSave = anchoredGameObject.GetComponent <WorldAnchor>();
                if (anchorToSave == null)
                {
                    anchorToSave = anchoredGameObject.AddComponent <WorldAnchor>();
                }
                //saves the anchor in the store
                AnchorStore.Save(anchoredGameObject.name, anchorToSave);



                break;

            case AnchorOperation.Load:
                //if there is an anchor for the go, then load it and attache it
                if (new List <string>(AnchorStore.GetAllIds()).Contains(anchoredGameObject.name))
                {
                    AnchorStore.Load(anchoredGameObject.name, anchoredGameObject);
                    anchorAttachmentInfo.onComplete?.Invoke(true);
                }
                else
                {
                    anchorAttachmentInfo.onComplete?.Invoke(false);
                }


                break;

            case AnchorOperation.Delete:
                //should not habben, because we do it directly in the methode
                removeAnchor(anchoredGameObject);
                break;

            default:
                throw new System.ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes the anchor operations from the localAnchorOperations queue.
        /// </summary>
        /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
        protected void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
        {
            string     anchorId           = anchorAttachmentInfo.AnchorName;
            GameObject anchoredGameObject = anchorAttachmentInfo.AnchoredGameObject;

            switch (anchorAttachmentInfo.Operation)
            {
            case AnchorOperation.Save:
                if (anchoredGameObject == null)
                {
                    Debug.LogError("[WorldAnchorManager] The GameObject referenced must have been destroyed before we got a chance to anchor it.");
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nThe GameObject referenced must have been destroyed before we got a chance to anchor it.";
                    }
                    break;
                }

                if (string.IsNullOrEmpty(anchorId))
                {
                    anchorId = anchoredGameObject.name;
                }

                // Try to load a previously saved world anchor.
                WorldAnchor savedAnchor = AnchorStore.Load(anchorId, anchoredGameObject);

                if (savedAnchor == null)
                {
                    // Check if we need to import the anchor.
                    if (ImportAnchor(anchorId, anchoredGameObject) == false)
                    {
                        if (ShowDetailedLogs)
                        {
                            Debug.LogFormat("[WorldAnchorManager] Anchor could not be loaded for \"{0}\". Creating a new anchor.", anchoredGameObject.name);
                        }

                        if (AnchorDebugText != null)
                        {
                            AnchorDebugText.text += string.Format("\nAnchor could not be loaded for \"{0}\". Creating a new anchor.", anchoredGameObject.name);
                        }

                        // Create anchor since one does not exist.
                        CreateAnchor(anchoredGameObject, anchorId);
                    }
                }
                else
                {
                    savedAnchor.name = anchorId;
                    if (ShowDetailedLogs)
                    {
                        Debug.LogFormat("[WorldAnchorManager] Anchor loaded from anchor store and updated for \"{0}\".", anchoredGameObject.name);
                    }

                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += string.Format("\nAnchor loaded from anchor store and updated for \"{0}\".", anchoredGameObject.name);
                    }
                }

                AnchorGameObjectReferenceList.Add(anchorId, anchoredGameObject);
                break;

            case AnchorOperation.Delete:
                if (AnchorStore == null)
                {
                    Debug.LogError("[WorldAnchorManager] Remove anchor called before anchor store is ready.");
                    break;
                }

                // If we don't have a GameObject reference, let's try to get the GameObject reference from our dictionary.
                if (!string.IsNullOrEmpty(anchorId) && anchoredGameObject == null)
                {
                    AnchorGameObjectReferenceList.TryGetValue(anchorId, out anchoredGameObject);
                }

                if (anchoredGameObject != null)
                {
                    var anchor = anchoredGameObject.GetComponent <WorldAnchor>();

                    if (anchor != null)
                    {
                        anchorId = anchor.name;
                        DestroyImmediate(anchor);
                    }
                    else
                    {
                        Debug.LogErrorFormat("[WorldAnchorManager] Unable remove WorldAnchor from {0}!", anchoredGameObject.name);
                        if (AnchorDebugText != null)
                        {
                            AnchorDebugText.text += string.Format("\nUnable remove WorldAnchor from {0}!", anchoredGameObject.name);
                        }
                    }
                }
                else
                {
                    Debug.LogError("[WorldAnchorManager] Unable find a GameObject to remove an anchor from!");
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nUnable find a GameObject to remove an anchor from!";
                    }
                }

                if (!string.IsNullOrEmpty(anchorId))
                {
                    AnchorGameObjectReferenceList.Remove(anchorId);
                    DeleteAnchor(anchorId);
                }
                else
                {
                    Debug.LogError("[WorldAnchorManager] Unable find an anchor to delete!");
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nUnable find an anchor to delete!";
                    }
                }

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }