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.

How to use unity world anchors?

I found documentation on it https://developer.microsoft.com/en-us/windows/holographic/world_anchor_in_unity

but it doesn't really say much. Putting the code on and item "WorldAnchor anchor = gameObject.AddComponent();" while syntactically correct, doesn't do anything. I can't find any way to learn how to get the anchors to actually function.

Answers

  • @SamJa

    To use a world anchor:

    First you need the mechanics to load the anchor(s) you are going to store

    include the following using statements in your script

    using UnityEngine.VR.WSA.Persistence;
    using UnityEngine.VR.WSA;
    

    in the Start method do an async call to Load the anchorstore

    WorldAnchorStore.GetAsync(AnchorStoreLoaded);

    create a handler for the AnchorStoreLoaded event

       private void AnchorStoreLoaded(WorldAnchorStore store)
        {
            this.store = store;
            LoadAnchors();
        }
    

    put the logic for loading the anchor(s) found in the anchorstore in a LoadAnchors() method

      private void LoadAnchors()
        {      
                    retTrue= this.store.Load("theGameObjectIWantAnchored", theGameObjectIWantAnchored);
                    if (!retTrue)
                    {
                        // Until the gameObjectIWantAnchored has an anchor saved at least once it will not be in the AnchorStore
                    }
        }
    

    Then you are ready to save an anchor to the anchorstore

      private void SaveAnchor()
        {
            bool retTrue;
            anchor = theGameObjectIWantAnchored.AddComponent<WorldAnchor>();
           // Remove any previous worldanchor saved with the same name so we can save new one
            this.store.Delete(theGameObjectIWantAnchored.name.ToString()); 
            retTrue = this.store.Save(theGameObjectIWantAnchored.name.ToString(), anchor);
            if (!retTrue)
            {
                Debug.Log("Anchor save failed.");
            }
        }
    

    You will also want to clear the anchor from the anchorstore each time before you move or place theGameObjectIWantAnchored with something like:

     private void ClearAnchor()
        {
            anchor = theGameObjectIWantAnchored.GetComponent<WorldAnchor>();
            if (anchor)
            {
                // remove any world anchor component from the game object so that it can be moved
                DestroyImmediate(anchor);
            }
        }
    

    and call that ClearAnchor method before any relevant

    theGameObjectIWantAnchored.transform.position = ....
    

    or similar calls that move the game object.

    You will likely also want to modify these or similar routines so that you can pass in different gameobjects that you want to have world anchors for, but this should get you started. Note you will likely need to inspect the anchorstore to compare against the anchors you care about once you get in to more complex multi anchor scenarios.

    HTH

    Windows Holographic User Group Redmond

    WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
    WinHUGR YouTube Channel -- live streamed meetings

  • @SamJa You can use the HoloToolKit-Unity which contains World Anchor Manager. https://github.com/Microsoft/HoloToolkit-Unity

  • @guowenbin90 said:
    @SamJa You can use the HoloToolKit-Unity which contains World Anchor Manager. https://github.com/Microsoft/HoloToolkit-Unity

    Can you give any tips or tutorial about using world anchors?
    Cant seem to figure this one out :/

  • SamJaSamJa
    edited February 2017

    @firativerson said:

    @guowenbin90 said:
    @SamJa You can use the HoloToolKit-Unity which contains World Anchor Manager. https://github.com/Microsoft/HoloToolkit-Unity

    Can you give any tips or tutorial about using world anchors?
    Cant seem to figure this one out :/

    Yeah after all this time I'm still trying to figure this one out. Everyone on the internet seems to think I know something that I don't starting out.

    Literally the only thing that I know is that my Unity scene contains an object called "SpatialMapping" which has a script in it called "Object Surface Observer" that has its "Room Model" variable mesh set to the room I have scanned. Knowing that I have the HoloToolkit, how on Earth to I anchor something to a specific spot in that room now?

  • @HoloSheep already gave you a fair good explanation what you have to do.

    For short you have to write a new script and attach it to the game object that you want to anchor. Every time after you have positioned your object you have to execute the SaveAnchor() method. If it was already anchored you have to call the ClearAnchor() method before you move it again. If you create an new object and want it anchored at a already saved position you have to call LoadAnchors().

    Your question is about anchoring an object and not about moving it, so I assume that you already can move an object. The code to move your object will be in a script, too. So you could hook your code into this movement script and execute the logic to anchor the object after the movement.

    If you are not familiar with writing scripts, this is a good starting point: https://docs.unity3d.com/Manual/ScriptingSection.html

  • You can use the HoloToolKit-Unity which contains World Anchor Manager.You just call the AttachAnchor function,like this WorldAnchorManager.Instance.AttchAnchor(gameObject,"HumanAnchor");

Sign In or Register to comment.