Skip to content

Commit 8776856

Browse files
spawning after onnetworkspawn sweep, gizmos for in-scene spawn data object
1 parent d08ea77 commit 8776856

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

Assets/Prefabs/Game/NetworkObjectSpawnData.prefab

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GameObject:
1313
m_Layer: 0
1414
m_Name: NetworkObjectSpawnData
1515
m_TagString: Untagged
16-
m_Icon: {fileID: 0}
16+
m_Icon: {fileID: 419385456094870383, guid: 0000000000000000d000000000000000, type: 0}
1717
m_NavMeshLayer: 0
1818
m_StaticEditorFlags: 0
1919
m_IsActive: 1

Packages/com.unity.multiplayer.samples.coop/Utilities/Net/NetworkObjectSpawner.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using Unity.Netcode;
@@ -11,7 +12,7 @@ namespace Unity.Multiplayer.Samples.Utilities
1112
/// <summary>
1213
/// Custom spawning component to be added to a scene GameObject. This component collects NetworkObjects in a scene
1314
/// marked by a special tag, collects their Transform data, destroys their prefab instance, and performs the dynamic
14-
/// spawning of said objects during Netcode for GameObject's OnNetworkSpawn() callback.
15+
/// spawning of said objects during Netcode for GameObject's (Netcode) OnNetworkSpawn() callback.
1516
/// </summary>
1617
[RequireComponent(typeof(NetcodeHooks))]
1718
public class NetworkObjectSpawner : MonoBehaviour
@@ -27,7 +28,7 @@ public class NetworkObjectSpawner : MonoBehaviour
2728

2829
const string k_NetworkObjectSpawnerCollectableTag = "NetworkObjectSpawnerCollectable";
2930

30-
void Start()
31+
void Awake()
3132
{
3233
m_NetcodeHooks.OnNetworkSpawnHook += OnNetworkSpawn;
3334
}
@@ -48,6 +49,13 @@ void OnNetworkSpawn()
4849
return;
4950
}
5051

52+
StartCoroutine(WaitToSpawnNetworkObjects());
53+
}
54+
55+
IEnumerator WaitToSpawnNetworkObjects()
56+
{
57+
// must wait for Netcode's OnNetworkSpawn() sweep before dynamically spawning
58+
yield return new WaitForEndOfFrame();
5159
SpawnNetworkObjects();
5260
}
5361

@@ -57,7 +65,8 @@ void SpawnNetworkObjects()
5765
{
5866
var spawnedGameObject = Instantiate(m_SpawnObjectData[i].prefabReference,
5967
m_SpawnObjectData[i].transform.position,
60-
m_SpawnObjectData[i].transform.rotation);
68+
m_SpawnObjectData[i].transform.rotation,
69+
null);
6170

6271
spawnedGameObject.transform.localScale = m_SpawnObjectData[i].transform.lossyScale;
6372
var spawnedNetworkObject = spawnedGameObject.GetComponent<NetworkObject>();
@@ -68,8 +77,7 @@ void SpawnNetworkObjects()
6877
}
6978

7079
#if UNITY_EDITOR
71-
[ContextMenu("Collect")]
72-
public void Collect()
80+
public void CollectTaggedPrefabInstances()
7381
{
7482
var prefabStage = PrefabStageUtility.GetPrefabStage(gameObject);
7583

@@ -121,7 +129,7 @@ public override void OnInspectorGUI()
121129
if (PrefabStageUtility.GetCurrentPrefabStage() &&
122130
GUILayout.Button("Collect tagged prefab instances"))
123131
{
124-
networkObjectSpawner.Collect();
132+
networkObjectSpawner.CollectTaggedPrefabInstances();
125133
}
126134
}
127135
}

0 commit comments

Comments
 (0)