Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.XR.WSA.Persistence;
  3. using UnityEngine.XR.WSA;
  4.  
  5.  // Make sure you have the following Scripts in in your Scene: SpatialMappingObserver, SpatialMappingManager
  6.  
  7. public class PersistantAnchorExample : MonoBehaviour
  8. {
  9.     public WorldAnchorStore anchorStore;
  10.     public GameObject AnchorRepresentation; // You have to set this to the Prefab you want
  11.  
  12.     void Start()
  13.     {
  14.         WorldAnchorStore.GetAsync(AnchorstoreReady); // On Start Load the Anchorstore
  15.     }
  16.  
  17.     private GameObject InstantiateObject()
  18.     {
  19.         GameObject NewAnchorRepresentation = UnityEngine.Object.Instantiate(AnchorRepresentation);
  20.         NewAnchorRepresentation.transform.position = Camera.main.transform.position + Camera.main.transform.forward;
  21.         return NewAnchorRepresentation;
  22.     }
  23.  
  24.     // Call this Function to Create a new Object with Anchor (For example from Voice Recognition)
  25.     public void CreateObjectWithAnchor()
  26.     {
  27.         GameObject NewAnchorRepresentation = InstantiateObject();
  28.         WorldAnchor newAnchor = NewAnchorRepresentation.AddComponent<WorldAnchor>();
  29.         string AnchorID = "Anchor" + UnityEngine.Random.Range(10000, 99999).ToString(); //Generate a random name for the Anchor
  30.         bool savedAnchor = false;
  31.         try
  32.         {
  33.             savedAnchor = anchorStore.Save(AnchorID, newAnchor); // Now the Anchor is saved across multiple sessions
  34.         }
  35.         catch (System.Exception e)
  36.         {
  37.             Debug.Log(e);
  38.             Debug.Log("Error Saving the Anchor");
  39.         }
  40.         if (!savedAnchor)
  41.         {
  42.             Debug.Log("Anchor failed to save to the store.");
  43.         }
  44.     }
  45.  
  46.     private void LoadAllAnchors()
  47.     {
  48.         if (anchorStore.anchorCount > 0)
  49.         {
  50.             string[] anchorIDs = anchorStore.GetAllIds();
  51.             foreach (string anchorID in anchorIDs)
  52.             {
  53.                 Debug.Log("Load " + anchorID);
  54.                 GameObject NewAnchorRepresentation = UnityEngine.Object.Instantiate(AnchorRepresentation);
  55.                 anchorStore.Load(anchorID, NewAnchorRepresentation); // This automatically sets the position of the object to the right position in the room
  56.             }
  57.         }
  58.         else
  59.         {
  60.             Debug.Log("Anchor Store is empty");
  61.         }
  62.     }
  63.  
  64.     // Callback from WorldAnchorStore.GetAsync
  65.     private void AnchorstoreReady(WorldAnchorStore store)
  66.     {
  67.         anchorStore = store;
  68.         LoadAllAnchors();
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement