Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.VR.WSA.Persistence;
- using UnityEngine.VR.WSA;
- // Additional Readings:
- // Unity has a website dedicated to Hololens, you should check it out!
- // https://unity3d.com/partners/microsoft/hololens
- // Even more reading:
- // https://forums.hololens.com/discussion/514/creating-and-assigning-spatial-anchors
- public class WorldAnchorStore : MonoBehaviour {
- WorldAnchorStore anchorStore;
- public string anchorStoreName;
- // This function condenses the loading of anchors into
- // gameobject and the store together.
- private void StoreLoaded(WorldAnchorStore store)
- {
- // We load the anchor store...
- anchorStore = store;
- // Load all of the IDs into an array of strings
- string[] ids = anchorStore.GetAllIds();
- // Loop through the entire array to find the object we want to load.
- for(int i = 0; i < ids.Length(); ++i)
- {
- if(ids[i] == anchorStoreName)
- {
- // Calling the load function should load the anchor onto
- // the gameobject.
- WorldAnchor results = anchorStore.Load(ids[i], this.gameObject);
- if(!results)
- Debug.Log("Loaded has failed for some reasons...");
- break;
- }
- }
- }
- // This function is called automatically when this script loads.
- void Start ()
- {
- // Function is called to run StoreLoaded.
- WorldAnchorStore.GetAsync(StoreLoaded);
- }
- // Use this function to perform any changes to the object.
- void Update ()
- {
- // E.g changing the movement
- // gameObject.transform.x += Time.deltaTime * speedFactor;
- }
- // Call this function when you want to save the anchor of the
- // game object!
- private void SaveAnchor()
- {
- WorldAnchor anchorToAttach = gameObject.AddComponent<WorldAnchor>();
- bool results = anchorStore.Saved(anchorStoreName, anchorToAttach);
- if(results == false)
- {
- Debug.Log("Attaching/Saving of anchor has failed!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement