forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnManager.cs
More file actions
126 lines (104 loc) · 4.07 KB
/
Copy pathSpawnManager.cs
File metadata and controls
126 lines (104 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Sharing.SyncModel;
namespace HoloToolkit.Sharing.Spawning
{
/// <summary>
/// A SpawnManager is in charge of spawning the appropriate objects based on changes to an array of data model objects
/// to which it is registered.
/// It also manages the lifespan of these spawned objects.
/// </summary>
/// <typeparam name="T">Type of SyncObject in the array being monitored by the SpawnManager.</typeparam>
public abstract class SpawnManager<T> : MonoBehaviour where T : SyncObject, new()
{
protected SharingStage NetworkManager { get; private set; }
protected SyncArray<T> SyncSource;
protected List<GameObject> SyncSpawnObjectListInternal = new List<GameObject>(0);
public bool IsSpawningObjects { get; protected set; }
public List<GameObject> SyncSpawnObjectList { get { return SyncSpawnObjectListInternal; } }
protected virtual void Start()
{
// SharingStage should be valid at this point, but we may not be connected.
NetworkManager = SharingStage.Instance;
if (NetworkManager.IsConnected)
{
Connected();
}
else
{
NetworkManager.SharingManagerConnected += Connected;
}
}
protected virtual void Connected(object sender = null, EventArgs e = null)
{
if (SyncSource != null)
{
IsSpawningObjects = true;
UnRegisterToDataModel();
for (var i = 0; i < SyncSpawnObjectListInternal.Count; i++)
{
Destroy(SyncSpawnObjectListInternal[i]);
}
SyncSpawnObjectListInternal.Clear();
}
SetDataModelSource();
RegisterToDataModel();
if (IsSpawningObjects)
{
ReSpawnObjects();
IsSpawningObjects = false;
}
}
/// <summary>
/// Sets the data model source for the spawn manager.
/// </summary>
protected abstract void SetDataModelSource();
/// <summary>
/// Register to data model updates;
/// </summary>
private void RegisterToDataModel()
{
SyncSource.ObjectAdded += OnObjectAdded;
SyncSource.ObjectRemoved += OnObjectRemoved;
}
private void UnRegisterToDataModel()
{
SyncSource.ObjectAdded -= OnObjectAdded;
SyncSource.ObjectRemoved -= OnObjectRemoved;
}
private void OnObjectAdded(T addedObject)
{
InstantiateFromNetwork(addedObject);
}
private void OnObjectRemoved(T removedObject)
{
RemoveFromNetwork(removedObject);
}
private void ReSpawnObjects()
{
T[] objs = SyncSource.GetDataArray();
for (var i = 0; i < objs.Length; i++)
{
InstantiateFromNetwork(objs[i]);
}
}
/// <summary>
/// Delete the data model for an object and all its related game objects.
/// </summary>
/// <param name="objectToDelete">Object that needs to be deleted.</param>
public abstract void Delete(T objectToDelete);
/// <summary>
/// Instantiate game objects based on data model that was created on the network.
/// </summary>
/// <param name="addedObject">Object that was added to the data model.</param>
protected abstract void InstantiateFromNetwork(T addedObject);
/// <summary>
/// Remove an object based on data model that was removed on the network.
/// </summary>
/// <param name="removedObject">Object that was removed from the data model.</param>
protected abstract void RemoveFromNetwork(T removedObject);
}
}