问题
As noted here, the documentation on this topic seems very limited. I know that I need a "World Anchor". I am having problems finding any documentation on how to use such an anchor. This is the closest documentation I can find from Microsoft, but leaves out a lot of details and doesn't have any working examples. The documentation from Unity isn't much better, although it addresses WorldAnchor
specifically.
I started by simply adding a component to the GameObject via the Unity editor, via "Add Component" > "AR" > "World Anchor". That did not seem to do anything.
Then I found this demo code from this YouTube video, and started implementing that (it is way more complicated than what I need).
I finally learned that there is a WorldAnchorManager
from this discussion, and attempted to use it via the code below. This also has had no effect:
using Microsoft.MixedReality.Toolkit.Experimental.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.WSA;
using UnityEngine.XR.WSA.Persistence;
public class HololensWorldAnchorManager : MonoBehaviour
{
private WorldAnchorManager Manager;
// Start is called before the first frame update
void Start()
{
// Load and get a reference to the store.
this.AttachAnchor();
}
private void AttachAnchor()
{
this.Manager = new WorldAnchorManager();
this.Manager.AttachAnchor(this.gameObject);
}
// more class stuff...
}
After following the directions here (which are out of date and DO NOT work anymore), I finally figured out how to make my project not in a window on the Hololens... but now without the anchors working, I see my hologram as if it is glued to the front of my face (and I also see the framerate bar even in the release version - I don't want that either).
Am I missing something? How do you actually use a world anchor? I just want my single hologram to stay put, in a physical space, and never move. Ideally, this location is set once before runtime and never changes. As a backup, the user placing the hologram once and then it staying there is acceptable. I don't even need persistence nor multiple users to see it nor multiple anchors...
UPDATE: I added a second object to the scene, and verified that moving with the Hololens on does not actually navigate around the scene at all. The anchors may be working fine, but I cannot verify this since the entire scene is moving with the user.
I also then tried outputting a Unity scene to the Hololens that is identical to the problem one, except the MRTK is not set up on it. This leads to the exact same result as the Hololens scene. Maybe I am missing a setup or build configuration step or something? The MRTK camera rig works fine in the Unity player... it is only on the Hololens that issues arise.
回答1:
Actually, the World Anchors
component will override their parent GameObject’s Transform
component, and any direct manipulations of the Transform component are lost. So, you should first determine the position of the hologram in the physical scene, and then add an anchor component in the runtime to remains locked in place to locations in the real world. For more detail about add/remove World Anchors operation on the Unity GameObject in C# Script, please refer to this link: Building a world-scale experience
Besides, for WorldAnchorManager
script, it is used to simplify usage of persistence WorldAnchor operations and can not meet your needs.
回答2:
Turns out the issue was indeed with the setup, and not the anchors (as mentioned in the UPDATE on the question). I had to follow the instructions here to finish upgrading my code to rely on the new XR plugin system Unity uses, instead of the legacy system.
I had not added the Tracked Pose Driver
component to the camera rig (under the "Migrating a Complex Scene" part of the linked directions), so the user wearing the Hololens was not driving the camera with their head movements and walking. That is why everything appeared to be "stuck" to the same view; the camera was never moving within the scene.
Once that was solved, the anchors worked beautifully from the link in the answer submitted by @Hernando - MSFT (linked again here).
来源:https://stackoverflow.com/questions/64345424/how-to-keep-hololens-holograms-anchored-in-physical-environment