From 2214d8e492f2f32f9f945cbe91d7bff26e228c35 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Mon, 3 Jul 2017 22:47:24 -0700 Subject: [PATCH 1/3] Added ability for Spawn Manager to respawn objects from the server on reconnection with its own internal list of spawned gameobject references. --- .../Scripts/Spawning/PrefabSpawnManager.cs | 4 +- .../Sharing/Scripts/Spawning/SpawnManager.cs | 44 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Assets/HoloToolkit/Sharing/Scripts/Spawning/PrefabSpawnManager.cs b/Assets/HoloToolkit/Sharing/Scripts/Spawning/PrefabSpawnManager.cs index 0fbaf880274..e7ef64e7046 100644 --- a/Assets/HoloToolkit/Sharing/Scripts/Spawning/PrefabSpawnManager.cs +++ b/Assets/HoloToolkit/Sharing/Scripts/Spawning/PrefabSpawnManager.cs @@ -42,8 +42,10 @@ public class PrefabSpawnManager : SpawnManager /// private int objectCreationCounter; - private void Awake() + protected override void Start() { + base.Start(); + InitializePrefabs(); } diff --git a/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs b/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs index f3d482139bb..9debb6a91d2 100644 --- a/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs +++ b/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs @@ -4,6 +4,7 @@ // using System; +using System.Collections.Generic; using UnityEngine; using HoloToolkit.Sharing.SyncModel; @@ -21,6 +22,14 @@ namespace HoloToolkit.Sharing.Spawning protected SyncArray SyncSource; + protected bool IsReSpawningObjects; + + public bool IsSpawningObjects { get { return IsReSpawningObjects; } } + + public List SyncSpawnObjectList { get { return SyncSpawnObjectListInternal; } } + + protected List SyncSpawnObjectListInternal = new List(); + protected virtual void Start() { // SharingStage should be valid at this point, but we may not be connected. @@ -37,10 +46,27 @@ protected virtual void Start() protected virtual void Connected(object sender = null, EventArgs e = null) { - NetworkManager.SharingManagerConnected -= Connected; + if (SyncSource != null) + { + IsReSpawningObjects = true; + UnRegesterToDataModel(); + + for (var i = 0; i < SyncSpawnObjectListInternal.Count; i++) + { + Destroy(SyncSpawnObjectListInternal[i]); + } + + SyncSpawnObjectListInternal.Clear(); + } SetDataModelSource(); RegisterToDataModel(); + + if (IsReSpawningObjects) + { + RespawnObjects(); + IsReSpawningObjects = false; + } } /// @@ -57,6 +83,12 @@ private void RegisterToDataModel() SyncSource.ObjectRemoved += OnObjectRemoved; } + private void UnRegesterToDataModel() + { + SyncSource.ObjectAdded -= OnObjectAdded; + SyncSource.ObjectRemoved -= OnObjectRemoved; + } + private void OnObjectAdded(T addedObject) { InstantiateFromNetwork(addedObject); @@ -67,6 +99,16 @@ 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]); + } + } + /// /// Delete the data model for an object and all its related game objects. /// From 141f5507e371d728da8fdb812e534426a9c55a63 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Mon, 3 Jul 2017 23:01:11 -0700 Subject: [PATCH 2/3] updated IsRespawning property --- .../Sharing/Scripts/Spawning/SpawnManager.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs b/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs index 9debb6a91d2..a4014665a4b 100644 --- a/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs +++ b/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs @@ -22,9 +22,7 @@ namespace HoloToolkit.Sharing.Spawning protected SyncArray SyncSource; - protected bool IsReSpawningObjects; - - public bool IsSpawningObjects { get { return IsReSpawningObjects; } } + public bool IsSpawningObjects { get; protected set; } public List SyncSpawnObjectList { get { return SyncSpawnObjectListInternal; } } @@ -48,7 +46,7 @@ protected virtual void Connected(object sender = null, EventArgs e = null) { if (SyncSource != null) { - IsReSpawningObjects = true; + IsSpawningObjects = true; UnRegesterToDataModel(); for (var i = 0; i < SyncSpawnObjectListInternal.Count; i++) @@ -62,10 +60,10 @@ protected virtual void Connected(object sender = null, EventArgs e = null) SetDataModelSource(); RegisterToDataModel(); - if (IsReSpawningObjects) + if (IsSpawningObjects) { RespawnObjects(); - IsReSpawningObjects = false; + IsSpawningObjects = false; } } From 43155062549f4f2c13e7dddd481cbb5b38d72615 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Mon, 3 Jul 2017 23:07:32 -0700 Subject: [PATCH 3/3] fixed a couple mispellings, and ordered fields and properties. --- .../Sharing/Scripts/Spawning/SpawnManager.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs b/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs index a4014665a4b..064f68edd6f 100644 --- a/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs +++ b/Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs @@ -22,12 +22,12 @@ namespace HoloToolkit.Sharing.Spawning protected SyncArray SyncSource; + protected List SyncSpawnObjectListInternal = new List(0); + public bool IsSpawningObjects { get; protected set; } public List SyncSpawnObjectList { get { return SyncSpawnObjectListInternal; } } - protected List SyncSpawnObjectListInternal = new List(); - protected virtual void Start() { // SharingStage should be valid at this point, but we may not be connected. @@ -47,7 +47,7 @@ protected virtual void Connected(object sender = null, EventArgs e = null) if (SyncSource != null) { IsSpawningObjects = true; - UnRegesterToDataModel(); + UnRegisterToDataModel(); for (var i = 0; i < SyncSpawnObjectListInternal.Count; i++) { @@ -62,7 +62,7 @@ protected virtual void Connected(object sender = null, EventArgs e = null) if (IsSpawningObjects) { - RespawnObjects(); + ReSpawnObjects(); IsSpawningObjects = false; } } @@ -81,7 +81,7 @@ private void RegisterToDataModel() SyncSource.ObjectRemoved += OnObjectRemoved; } - private void UnRegesterToDataModel() + private void UnRegisterToDataModel() { SyncSource.ObjectAdded -= OnObjectAdded; SyncSource.ObjectRemoved -= OnObjectRemoved; @@ -97,7 +97,7 @@ private void OnObjectRemoved(T removedObject) RemoveFromNetwork(removedObject); } - private void RespawnObjects() + private void ReSpawnObjects() { T[] objs = SyncSource.GetDataArray();