Hello everyone.

We have decided to phase out the Mixed Reality Forums over the next few months in favor of other ways to connect with us.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

The plan between now and the beginning of May is to clean up old, unanswered questions that are no longer relevant. The forums will remain open and usable.

On May 1st we will be locking the forums to new posts and replies. They will remain available for another three months for the purposes of searching them, and then they will be closed altogether on August 1st.

So, where does that leave our awesome community to ask questions? Well, there are a few places we want to engage with you. For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality. If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers. And always feel free to hit us up on Twitter @MxdRealityDev.

Loosing WorldAnchors when shifting from Unity editor to deployed version

cgvgcgvg
edited January 2017 in Questions And Answers

Hi,
I'll try to summarize as clearly as possible our problem. We are currently designing our application with Unity editor in remote mode, in order to fine-place our holograms into the room and then attach world anchors to them.

Everything works like a charm, we place our objects, we change their position, we restart everything after days and all our holograms are there in the the correct place.

But (there's always a but...), we tried deploying the application to the device and then all our anchors disappeared. The anchor store is totally empty. We start again from the editor, the store includes our anchors.

So, first question is: when working with the editor in remote mode, where are the anchor stored? Apparently, not on the device (hololens) but locally. If this supposition is correct, how can we access them and possibly use them in the deployed version?

Second question. Given the previous problem, we tried to serialize (from editor remote mode) our world anchors through the WorldAnchorTransferBatch.ExportAsync function to save them into a binary file and open them in the deployed version. However, we always get a SerializationCompletionReason.AccessDenied result. The question is: is it possible to use anchor serialization in editor remote mode (or any other method to do a custom serialization)?

Concluding, what are we doing wrong?
Thanks in advance for any possible help

Answers

  • cgvgcgvg
    edited January 2017

    Hi all,
    here attached you will find a script that allows to recreate the problem. You simply have to create a hololens project, add an empty object, give it a label and add the script to it. When the application starts, you will see a big sphere in front of you.
    Then, you can place the game object (not the sphere) from the Editor in the desired position and, pressing 's' key, you can add a world anchor and save it. Press 'c' to clear the world anchor and change the object position. At startup, if the anchor has been saved, it is loaded and the world anchor is restored.

    For debugging purposes, the sphere color reflects its state (white: sphere not locked, red: object anchor not loaded from the store, green: object anchor loaded from the store, blue: anchor not saved).

    When starting the application in remote mode from the editor everything works (once saved, the object is locked in the correct position in the following launches), when the application is deployed (after having saved the anchor), the sphere starts in red color, highlighting that the anchor could not be loaded.

    Again, any idea of what we are doing wrong?

    
    using UnityEngine;
    using UnityEngine.VR.WSA;
    using UnityEngine.VR.WSA.Persistence;
    
    public class AnchorPlacer : MonoBehaviour
    {
        private bool loadAnchorAtStartup = true;
        protected WorldAnchorStore store = null;
        protected GameObject sphere = null;
    
    
        protected void StoreLoaded(WorldAnchorStore store)
        {
          this.store = store;
          if(loadAnchorAtStartup)
            LoadAnchor();
        }
    
        protected void Update()
        {
          // s key to save object anchor
          if(Input.GetKeyDown("s"))
            SaveAnchor();
        
          
          // c key to clear object anchor, thus allowing to change its position from the Unity editor
          if(Input.GetKeyDown("c"))
          {
            WorldAnchor anchor = gameObject.GetComponent<WorldAnchor>();
            if(anchor != null)
                DestroyImmediate(anchor);
        
            SetColor(sphere, Color.white);
          }
        }
    
    
        protected void SetColor(GameObject obj,Color color)
        {
          Renderer renderer = obj.GetComponent<Renderer>();
          if(renderer == null)
            return;
        
          renderer.material.color = color;
        }
    
        protected void Start()
        {
          sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
          sphere.transform.parent = transform;
          SetColor(sphere, Color.white);
          WorldAnchorStore.GetAsync(StoreLoaded);
        }
    
        public bool SaveAnchor()
        {
          //do the cleanup: if an anchor already exists, destroy it (from the gameobject and the store)
          WorldAnchor anchor = gameObject.GetComponent<WorldAnchor>();
        
          if(anchor != null)
            DestroyImmediate(anchor);
        
          if(store != null)
          {         
            store.Delete(gameObject.name);
        
            //add a game anchor to current object
            anchor = gameObject.AddComponent<WorldAnchor>();
        
            //save the anchor to the store
            if(!store.Save(gameObject.name, anchor))
            {
                Debug.LogWarning(string.Format("Anchor {0} NOT saved", gameObject.name));
                SetColor(sphere, Color.blue);
            }
            else
            {
                Debug.Log(string.Format("Anchor {0} successfully saved", gameObject.name));
                SetColor(sphere, Color.green);
                return true; //return true, because everything went all right
            }
          }
          else
            Debug.LogWarning(string.Format("Anchor {0} NOT saved because there's no access to the store", gameObject.name));
        
          //if we're here, we failed somehow
          return false;
        }
    
        public bool LoadAnchor()
        {
          //load the anchor of this object if available
        
          if(store != null)
          {         
            //get current world anchor (if it doesn't exist, add it)
            WorldAnchor anchor = gameObject.GetComponent<WorldAnchor>();
        
            if(anchor != null)
                DestroyImmediate(anchor);
        
            //load the anchor from the store
            if(!store.Load(gameObject.name, gameObject))
            {
                Debug.LogWarning(string.Format("Anchor {0} NOT loaded", gameObject.name));
                SetColor(sphere, Color.red);
            }
            else
            {
                Debug.Log(string.Format("Anchor {0} successfully loaded", gameObject.name));
                SetColor(sphere, Color.green);
                return true; //return true, because everything went all right
            }
          }
          else
          {
            Debug.LogWarning(string.Format("Anchor {0} NOT loaded because there's no access to the store", gameObject.name));
            SetColor(sphere, Color.yellow);
          }
        
          //if we're here, we failed somehow
          return false;
        }
    }
    
Sign In or Register to comment.