Hololens:Unity Player与已部署的应用程序之间的WorldAnchors问题

时间:2017-01-30 10:32:37

标签: c# unity3d hololens

我们正在使用Unity编辑器以远程模式设计我们的hololens应用程序,以便将我们的全息图精细放置到房间中,然后在完成后将世界锚点附加到它们。 在编辑器中一切正常(我们放置对象,我们改变它们的位置,我们在几天之后重新启动所有东西,我们所有的全息图都在正确的位置)。

但是当我们在设备上部署我们的应用程序时,我们所有的锚点都无法从锚点存储中加载。我们从编辑开始,商店包括我们的锚点。

所以,第一个问题是:在远程模式下使用编辑器时,锚存储在哪里?显然,不是在设备(hololens)上,而是在本地。如果这个假设是正确的,我们如何访问它们并可能在已部署的版本中使用它们?

第二个问题是,我们做错了吗?

这是一个可以重现问题的简短脚本。只需创建一个空对象,为其命名并附加脚本即可。应用程序启动时,会创建一个白色球体。您可以移动空并将其放在编辑器中,然后按“s”键保存锚点。在启动时,sript尝试从商店加载锚点。球体颜色告诉你它的状态(白色:球体解锁,绿色:锚点保存或从商店加载,红色:从商店加载的锚点没有成功,蓝色:在商店保存锚点没有成功)。从编辑器远程模式开始,保存您的锚点(锚点应该是绿色并且在下一次执行时处于相同位置)然后部署应用程序(您应该在相机前面看到红色球体,无论您在启动时查看方向)。

提前感谢您提供任何可能的帮助



    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;
        }
    }

0 个答案:

没有答案