HoloLens Development notes, hololens notes
The World Anchor space Anchor provides a way to retain an object at a specific position and in a rotating state. This ensures the stability of holographic objects and provides the ability to maintain the location of holographic objects in the real world. Simply put, you can add a space anchor for a holographic object, so that the holographic object can be accurately restored to its original position in subsequent steps.
WorldAnchor anchor = gameObject. AddComponent <WorldAnchor> ();
Removing a World Anchor remove a space Anchor If you no longer want to fix the GameObject in a specific location and do not want to move it in the scenario, you can call the Destroy () method to Destroy the WorldAnchor component, as shown below:
Destroy(gameObject.GetComponent<WorldAnchor>());
If you want to move objects in the scene immediately, you need to call DestroyImmediate () to destroy the space anchor component, as shown below:
DestroyImmediate(gameObject.GetComponent<WorldAnchor>());
Moving a World Anchored GameObject When a holographic object is attached with a space anchor, it cannot be moved. If you need a holographic object, you must:
DestroyImmediate(gameObject.GetComponent<WorldAnchor>());gameObject.transform.position = new Vector3(0, 0, 2);WorldAnchor anchor = gameObject.AddComponent<WorldAnchor>();
Handling Locatability Changes At a certain point in time, space anchors may not be located in the world. If this happens, Unity cannot update the position of the anchored object. This may change when the application is running. If you cannot promptly process changes in the positioning capability, the object may not appear in an accurate position.
In order to track changes in the ability to be located, the following actions must be taken:
Subscribe to eventsThe Code is as follows:
anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
Handle OnTrackingChanged eventsThe Code is as follows:
Private void Anchor_OnTrackingChanged (WorldAnchor self, bool located) {// display or hide the object self. gameObject. SetActiveRecursively (located) based on the positioning status );}
Set a clear initial statusSometimes space anchors can be located immediately. At this time, after a space anchor is added to an object, the isLocated attribute value of the Space anchor component is set to true, which means the OnTrackingChanged event will not be triggered. Therefore, we recommend that you use the initial isLocated status to call the OnTrackingChanged event immediately after adding the space anchor component, as shown below:
Anchor_OnTrackingChanged(anchor, anchor.isLocated);