AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SpawnManager.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;
5 using System.Collections.Generic;
6 using UnityEngine;
8 
9 namespace HoloToolkit.Sharing.Spawning
10 {
17  public abstract class SpawnManager<T> : MonoBehaviour where T : SyncObject, new()
18  {
19  protected SharingStage NetworkManager { get; private set; }
20 
22 
23  protected List<GameObject> SyncSpawnObjectListInternal = new List<GameObject>(0);
24 
25  public bool IsSpawningObjects { get; protected set; }
26 
27  public List<GameObject> SyncSpawnObjectList { get { return SyncSpawnObjectListInternal; } }
28 
29  protected virtual void Start()
30  {
31  // SharingStage should be valid at this point, but we may not be connected.
32  NetworkManager = SharingStage.Instance;
33  if (NetworkManager.IsConnected)
34  {
35  Connected();
36  }
37  else
38  {
39  NetworkManager.SharingManagerConnected += Connected;
40  }
41  }
42 
43  protected virtual void Connected(object sender = null, EventArgs e = null)
44  {
45  if (SyncSource != null)
46  {
47  IsSpawningObjects = true;
48  UnRegisterToDataModel();
49 
50  for (var i = 0; i < SyncSpawnObjectListInternal.Count; i++)
51  {
52  Destroy(SyncSpawnObjectListInternal[i]);
53  }
54 
55  SyncSpawnObjectListInternal.Clear();
56  }
57 
58  SetDataModelSource();
59  RegisterToDataModel();
60 
61  if (IsSpawningObjects)
62  {
63  ReSpawnObjects();
64  IsSpawningObjects = false;
65  }
66  }
67 
71  protected abstract void SetDataModelSource();
72 
76  private void RegisterToDataModel()
77  {
78  SyncSource.ObjectAdded += OnObjectAdded;
79  SyncSource.ObjectRemoved += OnObjectRemoved;
80  }
81 
82  private void UnRegisterToDataModel()
83  {
84  SyncSource.ObjectAdded -= OnObjectAdded;
85  SyncSource.ObjectRemoved -= OnObjectRemoved;
86  }
87 
88  private void OnObjectAdded(T addedObject)
89  {
90  InstantiateFromNetwork(addedObject);
91  }
92 
93  private void OnObjectRemoved(T removedObject)
94  {
95  RemoveFromNetwork(removedObject);
96  }
97 
98  private void ReSpawnObjects()
99  {
100  T[] objs = SyncSource.GetDataArray();
101 
102  for (var i = 0; i < objs.Length; i++)
103  {
104  InstantiateFromNetwork(objs[i]);
105  }
106  }
107 
112  public abstract void Delete(T objectToDelete);
113 
118  protected abstract void InstantiateFromNetwork(T addedObject);
119 
124  protected abstract void RemoveFromNetwork(T removedObject);
125  }
126 }
Action< T > ObjectRemoved
Called when an existing object has been removed from the array
Definition: SyncArray.cs:27
The SyncArray class provides the functionality of an array in the data model. The array holds entire ...
Definition: SyncArray.cs:17
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 SyncObject class is a container object that can hold multiple SyncPrimitives. ...
Definition: SyncObject.cs:21
Action< T > ObjectAdded
Called when a new object has been added to the array.
Definition: SyncArray.cs:22
A SpawnManager is in charge of spawning the appropriate objects based on changes to an array of data ...
Definition: SpawnManager.cs:17
virtual void Connected(object sender=null, EventArgs e=null)
Definition: SpawnManager.cs:43
The SharingStage is in charge of managing the core networking layer for the application.
Definition: SharingStage.cs:14