AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
FileSurfaceObserver.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 #if UNITY_EDITOR
8 using UnityEditor;
9 #endif
10 
11 namespace HoloToolkit.Unity.SpatialMapping
12 {
14  {
15  [Tooltip("The file name to use when saving and loading meshes.")]
16  public string MeshFileName = "roombackup";
17 
18  [Tooltip("Key to press in editor to load a spatial mapping mesh from a .room file.")]
19  public KeyCode LoadFileKey = KeyCode.L;
20 
21  [Tooltip("Key to press in editor to save a spatial mapping mesh to file.")]
22  public KeyCode SaveFileKey = KeyCode.S;
23 
28  public void Load(string fileName)
29  {
30  if (string.IsNullOrEmpty(fileName))
31  {
32  Debug.Log("No mesh file specified.");
33  return;
34  }
35 
36  Cleanup();
37 
38  try
39  {
40  IList<Mesh> storedMeshes = MeshSaver.Load(fileName);
41 
42  for(int iMesh = 0; iMesh < storedMeshes.Count; iMesh++)
43  {
44  AddSurfaceObject(CreateSurfaceObject(
45  mesh: storedMeshes[iMesh],
46  objectName: "storedmesh-" + iMesh,
47  parentObject: transform,
48  meshID: iMesh
49  ));
50  }
51  }
52  catch
53  {
54  Debug.Log("Failed to load " + fileName);
55  }
56  }
57 
58  // Called every frame.
59  private void Update()
60  {
61  // Keyboard commands for saving and loading a remotely generated mesh file.
62 #if UNITY_EDITOR || UNITY_STANDALONE
63  // S - saves the active mesh
64  if (Input.GetKeyUp(SaveFileKey))
65  {
66  MeshSaver.Save(MeshFileName, SpatialMappingManager.Instance.GetMeshes());
67  }
68 
69  // L - loads the previously saved mesh into editor and sets it to be the spatial mapping source.
70  if (Input.GetKeyUp(LoadFileKey))
71  {
72  SpatialMappingManager.Instance.SetSpatialMappingSource(this);
73  Load(MeshFileName);
74  }
75 #endif
76  }
77  }
78 
79 #if UNITY_EDITOR
80  [CustomEditor(typeof(FileSurfaceObserver))]
81  public class FileSurfaceObserverEditor : Editor
82  {
83  public override void OnInspectorGUI()
84  {
85  base.OnInspectorGUI();
86 
87  // Quick way for the user to get access to the room file location.
88  if (GUILayout.Button("Open File Location"))
89  {
90  System.Diagnostics.Process.Start(MeshSaver.MeshFolderName);
91  }
92  }
93  }
94 #endif
95 }
MeshSaver is a static class containing methods used for saving and loading meshes.
Definition: MeshSaver.cs:20
static string MeshFolderName
Read-only property which returns the folder path where mesh files are stored.
Definition: MeshSaver.cs:31
static IList< Mesh > Load(string fileName)
Loads the specified mesh file.
Definition: MeshSaver.cs:120
static string Save(string fileName, IEnumerable< MeshFilter > meshFilters)
Transforms all the mesh vertices into world position before saving to file.
Definition: MeshSaver.cs:49
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
The SpatialMappingManager class allows applications to use a SurfaceObserver or a stored Spatial Mapp...
void Load(string fileName)
Loads the SpatialMapping mesh from the specified file.