diff --git a/Basic/ClientDriven/Assets/Scenes/Bootstrap.unity b/Basic/ClientDriven/Assets/Scenes/Bootstrap.unity
index eabf639dd..dd474bd17 100644
--- a/Basic/ClientDriven/Assets/Scenes/Bootstrap.unity
+++ b/Basic/ClientDriven/Assets/Scenes/Bootstrap.unity
@@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
- m_IndirectSpecularColor: {r: 0.21890283, g: 0.16790575, b: 0.67034173, a: 1}
+ m_IndirectSpecularColor: {r: 0.21890238, g: 0.16790517, b: 0.6703396, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
@@ -925,6 +925,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
GlobalObjectIdHash: 2084257867
+ InScenePlacedSourceGlobalObjectIdHash: 0
AlwaysReplicateAsRoot: 0
SynchronizeTransform: 1
ActiveSceneSynchronization: 0
@@ -1525,6 +1526,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
GlobalObjectIdHash: 1374255023
+ InScenePlacedSourceGlobalObjectIdHash: 0
AlwaysReplicateAsRoot: 0
SynchronizeTransform: 1
ActiveSceneSynchronization: 0
@@ -1550,7 +1552,7 @@ MonoBehaviour:
- {fileID: 1422708175}
- {fileID: 796975752}
m_SpawnRatePerSecond: 10
- m_IngredientPrefab: {fileID: 8321201880322001125, guid: a6b33b41508134c09957e076f4d53415, type: 3}
+ m_IngredientPrefab: {fileID: 5818429371130516787, guid: a6b33b41508134c09957e076f4d53415, type: 3}
m_MaxSpawnWaves: 8
--- !u!4 &2060465724
Transform:
diff --git a/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs b/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs
index b7b8a5948..fe8e9672b 100644
--- a/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs
+++ b/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs
@@ -9,7 +9,7 @@ namespace Unity.Multiplayer.Samples.ClientDriven
///
/// A NetworkManager is expected to be part of the scene that this NetworkObject is a part of.
///
- internal class NetworkObjectSpawner : MonoBehaviour
+ class NetworkObjectSpawner : MonoBehaviour
{
[SerializeField]
NetworkObject m_PrefabReference;
@@ -27,17 +27,20 @@ void Start()
void OnDestroy()
{
- if(NetworkManager.Singleton != null)
- {
+ if (NetworkManager.Singleton != null)
+ {
NetworkManager.Singleton.OnServerStarted -= SpawnIngredient;
}
}
void SpawnIngredient()
{
+ // Note: this will be converted to NetworkObject.InstantiateAndSpawn(), but a current limitation on Netcode
+ // for GameObjects invoking this method on OnServerStarted prevents this API upgrade.
+ // Specifically, if you were to spawn a Rigidbody with Rigidbody Interpolation enabled, you would need to
+ // update the Rigidbody's position immediately after invoking NetworkObject.InstantitateAndSpawn().
NetworkObject instantiatedNetworkObject = Instantiate(m_PrefabReference, transform.position, transform.rotation, null);
- var ingredient = instantiatedNetworkObject.GetComponent();
- ingredient.NetworkObject.Spawn();
+ instantiatedNetworkObject.Spawn();
}
}
}
diff --git a/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs b/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs
index 631cf83a9..444f747e4 100644
--- a/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs
+++ b/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs
@@ -12,7 +12,7 @@ public class ServerIngredientSpawner : NetworkBehaviour
float m_SpawnRatePerSecond;
[SerializeField]
- GameObject m_IngredientPrefab;
+ NetworkObject m_IngredientPrefab;
[SerializeField]
int m_MaxSpawnWaves;
@@ -26,9 +26,9 @@ public class ServerIngredientSpawner : NetworkBehaviour
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
+ enabled = IsServer;
if (!IsServer)
{
- enabled = false;
return;
}
@@ -38,11 +38,12 @@ public override void OnNetworkSpawn()
public override void OnNetworkDespawn()
{
m_SpawnWaves = 0;
+ enabled = false;
}
void FixedUpdate()
{
- if (NetworkManager != null && !IsServer)
+ if (NetworkManager == null || !NetworkManager.IsListening || !IsServer)
{
return;
}
@@ -51,11 +52,10 @@ void FixedUpdate()
{
foreach (var spawnPoint in m_SpawnPoints)
{
- var newIngredientObject = Instantiate(m_IngredientPrefab, spawnPoint.transform.position, spawnPoint.transform.rotation);
- newIngredientObject.transform.position = spawnPoint.transform.position +
- new Vector3(UnityEngine.Random.Range(-0.25f, 0.25f), 0, UnityEngine.Random.Range(-0.25f, 0.25f));
+ var newIngredientObject = m_IngredientPrefab.InstantiateAndSpawn(NetworkManager,
+ position: spawnPoint.transform.position + new Vector3(UnityEngine.Random.Range(-0.25f, 0.25f), 0, UnityEngine.Random.Range(-0.25f, 0.25f)),
+ rotation: spawnPoint.transform.rotation);
var ingredient = newIngredientObject.GetComponent();
- ingredient.NetworkObject.Spawn();
ingredient.currentIngredientType.Value = (IngredientType)m_RandomGenerator.Next((int)IngredientType.MAX);
}
m_SpawnWaves++;
diff --git a/Basic/ClientDriven/README.md b/Basic/ClientDriven/README.md
index d23195613..7fb5d2e15 100644
--- a/Basic/ClientDriven/README.md
+++ b/Basic/ClientDriven/README.md
@@ -4,7 +4,7 @@
# Client Driven
[](https://unity.com/releases/editor/whats-new/2022.3.0)
-[](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases/tag/ngo%2F1.7.1)
+[](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases/tag/ngo%2F1.8.1)
[](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize/releases/tag/v1.5.0)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ca344ebce..410302eea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,7 @@
- Upgraded to Netcode for GameObjects v1.8.1 (#164)
- Upgraded to the newer API for Rpcs, Universal Rpcs
- The place of execution for a client's position was moved to ClientNetworkTransform child class, ClientDrivenNetworkTransform. This ensures no race condition issues on a client's first position sync. Server code now modifies a NetworkVariable that client-owned instances of ClientDrivenNetworkTransform use on OnNetworkSpawn to initially move a player
+ - Upgraded to use NetworkObject.InstantiateAndSpawn() API where appropriate (#173)
- Upgraded to IDE Rider v3.0.28 (#166)
- Upgraded to Unity 2022.3.27f1 (#175)
- com.unity.render-pipelines.core upgraded to v14.0.11