Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

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.

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, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

World Anchor problem- persistence not working

Hi! I am using a slightly modified version of @Patrick 's script for placing world anchors. I'm able to set my anchor by tapping to select, having it follow my gaze, then tapping again to place however when I close and reopen the app, everything restarts and I have to re-place the anchor. Can someone please tell me how to change my code so that the anchor's position persists between uses of the app? Thank you.

using UnityEngine;
using System.Collections;
using UnityEngine.VR.WSA.Persistence;
using UnityEngine.VR.WSA;

public class PersistoMatic : MonoBehaviour
{

private int tapCount;
public string ObjectAnchorStoreName;
private Color selectColor = new Color(1f, 0f, 0f, 0.2f);
private Color original;
private Color second = new Color(1f, 0.92f, 0.016f, 0.2f);
Color hitColor = new Color(0f, 1f, 0f, 0.2f);
private bool isRunning;
private bool placed;

WorldAnchorStore anchorStore;

public bool Placing = false;
// Use this for initialization
void Start()
{
    placed = false;
    tapCount = 0;
    original = this.gameObject.GetComponent<Renderer>().material.color;
    WorldAnchorStore.GetAsync(AnchorStoreReady);
}

void AnchorStoreReady(WorldAnchorStore store)
{
    anchorStore = store;
    //  Placing = true;

    Debug.Log("looking for " + ObjectAnchorStoreName);
    string[] ids = anchorStore.GetAllIds();
    for (int index = 0; index < ids.Length; index++)
    {
        Debug.Log(ids[index]);
        if (ids[index] == ObjectAnchorStoreName)
        {
            WorldAnchor wa = anchorStore.Load(ids[index], gameObject);
            Placing = false;
            break;
        }
    }
}

// Update is called once per frame
void Update()
{
    gameObject.GetComponent<Renderer>().material.color = selectColor;
    if (Placing)
    {
        // Do a raycast into the world that will only hit the Spatial Mapping mesh.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo,
            30.0f, SpatialMapping.PhysicsRaycastMask))
        {
            // Move this object's parent object to
            // where the raycast hit the Spatial Mapping mesh.
            Vector3 newPos = hitInfo.point;
            newPos.y += 0.1f;
            this.transform.position = newPos;



            // Rotate this object's parent object to face the user.
            Quaternion toQuat = Camera.main.transform.localRotation;
            toQuat.x = 0;
            toQuat.z = 0;
            this.transform.rotation = toQuat;
        }
        //  gameObject.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2;
    }
    else if (!Placing)
    {
        //gameObject.GetComponent<Renderer>().material.color = original;
      /*  if (placed)
        {
            //add rigidbody
            var rigidbody = this.gameObject.AddComponent<Rigidbody>();
            rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
        }*/
    }
}

void OnSelect()
{
    tapCount++;
   /* if (tapCount == 1)
    {
        Placing = true;
    }*/
    if (tapCount >= 2)
    {
        this.gameObject.GetComponent<Collider>().enabled = false;
        //this.gameObject.GetComponent<Renderer>().material.color = second;
        this.gameObject.GetComponent<MeshRenderer>().enabled = false;
    }
    if (anchorStore == null)
    {
        return;
    }

    if (Placing)
    {
        WorldAnchor attachingAnchor = gameObject.AddComponent<WorldAnchor>();
        if (attachingAnchor.isLocated)
        {
            Debug.Log("Saving persisted position immediately");
            bool saved = anchorStore.Save(ObjectAnchorStoreName, attachingAnchor);
            Debug.Log("saved: " + saved);
        }
        else
        {
            attachingAnchor.OnTrackingChanged += AttachingAnchor_OnTrackingChanged;
        }
    }
    else
    {
        WorldAnchor anchor = gameObject.GetComponent<WorldAnchor>();
        if (anchor != null)
        {
            DestroyImmediate(anchor);
        }

        string[] ids = anchorStore.GetAllIds();
        for (int index = 0; index < ids.Length; index++)
        {
            Debug.Log(ids[index]);
            if (ids[index] == ObjectAnchorStoreName)
            {
                bool deleted = anchorStore.Delete(ids[index]);
                Debug.Log("deleted: " + deleted);
                break;
            }
        }
    }

   Placing = !Placing;
}

private void AttachingAnchor_OnTrackingChanged(WorldAnchor self, bool located)
{
    if (located)
    {
        Debug.Log("Saving persisted position in callback");
        bool saved = anchorStore.Save(ObjectAnchorStoreName, self);
        Debug.Log("saved: " + saved);
        self.OnTrackingChanged -= AttachingAnchor_OnTrackingChanged;
    }
}

}

Tagged:

Comments

  • Also, is there a reason world anchor placement isn't working in Debug mode? When I deploy the app using release I'm able to place the anchor but when i have it in Debug mode so I can see my debug.log statements in the Output, the program crashes upon my placement "air tap" for setting down the anchor. Every time this error message pops up and leads me to this same line within the code but I don't understand the issue, and why this only crashes the program while I'm in the Debug mode:

    ![](http://cd8ba0b44a15c10065fd-24461f391e20b7336331d5789078af53.r23.cf1.rackcdn.com/hl.vanillacommunity.com
    /editor/cc/4j4heazsvyfz.jpg "")

    Any suggestions would be great, thank you!

  • if anyone else has this issue I think my problem was not actually naming the anchor. I set a custom string and persistence seems to work now

  • Ack, sorry I hadn't seen this and your PM. Looks like you figured it out.

    I do want to warn everyone about a bug we recently found in VS that will contribute to persistence not working. If you are deploying over Wi-Fi, in some configurations, VS will create a clean layout every other launch. If you are seeing this you can deploy over USB (use Device in VS) as a workaround. The VS team knows about the bug and is working on getting a fix made available.

    ===
    This post provided as-is with no warranties and confers no rights. Using information provided is done at own risk.

    (Daddy, what does 'now formatting drive C:' mean?)

Sign In or Register to comment.