- Home /
Spatial Anchors not saving correctly???
However I am having the greatest difficulty getting the Spatial Anchors to save except when another Instantiate is created, however it is supposed to save the Anchor once the prefab has stopped being dragged. (been working on it for a few days now :( ) Any help would mean the world to me.
This script is attached to the prefab along with the HandDraggable script. using HoloToolkit.Unity; using HoloToolkit.Unity.InputModule; using UnityEngine;
public class AnchorAdjustment : MonoBehaviour { static int sphereCount; int sphereNumber;
void Start()
{
this.gameObject.GetComponent<HandDraggable>().StartedDragging += OnStartedDragging;
this.gameObject.GetComponent<HandDraggable>().StoppedDragging += OnStoppedDragging;
this.sphereNumber = ++sphereCount;
}
void OnStartedDragging()
{
// When we start dragging we clear out any existing world anchor for this
// Xmas present.
WorldAnchorManager.Instance.RemoveAnchor(this.gameObject);
// We also add the RigidBody component so that (e.g.) you can't push
// the present through the floor.
var rigidBody = this.gameObject.AddComponent<Rigidbody>();
// But we don't want it to fall on the floor on its own.
rigidBody.useGravity = false;
}
void OnStoppedDragging()
{
// We take away the RigidBody because it doesn't play well with
// Rigidbody.
Destroy(this.gameObject.GetComponent<Rigidbody>());
// Now remember where the present was.
WorldAnchorManager.Instance.AttachAnchor(this.gameObject, this.sphereNumber.ToString());
}
void OnCollisionEnter(Collision collision)
{
// If we do get a collision then we (a bit rudley) just stop the
// drag operation.
this.gameObject.GetComponent<HandDraggable>().StopDragging();
}
}
This is the code that is attached to the manager that instantiates the prefab using HoloToolkit.Unity; using HoloToolkit.Unity.InputModule; using UnityEngine;
public class Anchor2 : MonoBehaviour, IInputClickHandler { public Transform prefab;
private void Start()
{
InputManager.Instance.PushFallbackInputHandler(
this.gameObject);
}
private void Update()
{
if (!this.loaded && (WorldAnchorManager.Instance.AnchorStore != null))
{
var ids = WorldAnchorManager.Instance.AnchorStore.GetAllIds();
// NB: I'm assuming that the ordering here is either preserved or
// maybe doesn't matter.
foreach (var id in ids)
{
var instance = Instantiate(this.prefab);
WorldAnchorManager.Instance.AttachAnchor(instance.gameObject, id);
}
this.loaded = true;
}
}
public void OnInputClicked(InputClickedEventData eventData)
{
var instance = Instantiate(this.prefab);
instance.gameObject.transform.position =
GazeManager.Instance.GazeOrigin +
GazeManager.Instance.GazeNormal * 1.5f;
}
bool loaded;
}