Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpawnManager Update #752

Merged
merged 4 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public class PrefabSpawnManager : SpawnManager<SyncSpawnedObject>
/// </summary>
private int objectCreationCounter;

private void Awake()
protected override void Start()
{
base.Start();

InitializePrefabs();
}

Expand Down
42 changes: 41 additions & 1 deletion Assets/HoloToolkit/Sharing/Scripts/Spawning/SpawnManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Sharing.SyncModel;

Expand All @@ -21,6 +22,12 @@ namespace HoloToolkit.Sharing.Spawning

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.
Expand All @@ -37,10 +44,27 @@ protected virtual void Start()

protected virtual void Connected(object sender = null, EventArgs e = null)
{
NetworkManager.SharingManagerConnected -= Connected;
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>
Expand All @@ -57,6 +81,12 @@ private void RegisterToDataModel()
SyncSource.ObjectRemoved += OnObjectRemoved;
}

private void UnRegisterToDataModel()
{
SyncSource.ObjectAdded -= OnObjectAdded;
SyncSource.ObjectRemoved -= OnObjectRemoved;
}

private void OnObjectAdded(T addedObject)
{
InstantiateFromNetwork(addedObject);
Expand All @@ -67,6 +97,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]);
}
}

/// <summary>
/// Delete the data model for an object and all its related game objects.
/// </summary>
Expand Down