Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.VR.WSA.Persistence;
  5. using UnityEngine.VR.WSA;
  6.  
  7. // Additional Readings:
  8. // Unity has a website dedicated to Hololens, you should check it out!
  9. // https://unity3d.com/partners/microsoft/hololens
  10.  
  11. // Even more reading:
  12. // https://forums.hololens.com/discussion/514/creating-and-assigning-spatial-anchors
  13.  
  14. public class WorldAnchorStore : MonoBehaviour {
  15.  
  16.     WorldAnchorStore anchorStore;
  17.     public string anchorStoreName;
  18.  
  19.     // This function condenses the loading of anchors into
  20.     // gameobject and the store together.
  21.     private void StoreLoaded(WorldAnchorStore store)
  22.     {
  23.        // We load the anchor store...
  24.        anchorStore = store;
  25.        // Load all of the IDs into an array of strings
  26.        string[] ids = anchorStore.GetAllIds();
  27.        // Loop through the entire array to find the object we want to load.
  28.        for(int i = 0; i < ids.Length(); ++i)
  29.        {
  30.          if(ids[i] == anchorStoreName)
  31.          {
  32.            // Calling the load function should load the anchor onto
  33.            // the gameobject.
  34.            WorldAnchor results = anchorStore.Load(ids[i], this.gameObject);
  35.            if(!results)
  36.              Debug.Log("Loaded has failed for some reasons...");
  37.            break;
  38.          }
  39.        }
  40.     }
  41.      
  42.     // This function is called automatically when this script loads.
  43.     void Start ()
  44.     {
  45.       // Function is called to run StoreLoaded.
  46.        WorldAnchorStore.GetAsync(StoreLoaded);
  47.     }
  48.    
  49.     // Use this function to perform any changes to the object.
  50.     void Update ()
  51.     {
  52.        // E.g changing the movement
  53.        // gameObject.transform.x += Time.deltaTime * speedFactor;
  54.     }
  55.    
  56.     // Call this function when you want to save the anchor of the
  57.     // game object!
  58.     private void SaveAnchor()
  59.     {
  60.       WorldAnchor anchorToAttach = gameObject.AddComponent<WorldAnchor>();
  61.       bool results = anchorStore.Saved(anchorStoreName, anchorToAttach);
  62.       if(results == false)
  63.       {
  64.         Debug.Log("Attaching/Saving of anchor has failed!");
  65.       }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement