From f8f73d51d2286a2efb3318b6de79d3e002f76c69 Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Mon, 6 May 2024 16:16:54 -0400 Subject: [PATCH 1/4] using NetworkObject.InstantiateAndSpawn() where possible --- Basic/ClientDriven/Assets/Scenes/Bootstrap.unity | 6 ++++-- .../Assets/Scripts/NetworkObjectSpawner.cs | 11 ++++++----- .../Assets/Scripts/ServerIngredientSpawner.cs | 11 +++++------ 3 files changed, 15 insertions(+), 13 deletions(-) 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..237d02237 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,18 @@ 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 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..058dcd808 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; @@ -42,7 +42,7 @@ public override void OnNetworkDespawn() void FixedUpdate() { - if (NetworkManager != null && !IsServer) + if (NetworkManager != null && !NetworkManager.IsListening && !IsServer) { return; } @@ -51,11 +51,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++; From 827926cf4c9760c66f915b1cd4dcd394fe2e3951 Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Wed, 8 May 2024 12:38:12 -0400 Subject: [PATCH 2/4] further clarifying InstantiateAndSpawn() issue --- Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs | 4 +++- Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs b/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs index 237d02237..fe8e9672b 100644 --- a/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs +++ b/Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs @@ -36,7 +36,9 @@ void OnDestroy() 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 + // 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); instantiatedNetworkObject.Spawn(); } diff --git a/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs b/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs index 058dcd808..444f747e4 100644 --- a/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs +++ b/Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs @@ -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 && !NetworkManager.IsListening && !IsServer) + if (NetworkManager == null || !NetworkManager.IsListening || !IsServer) { return; } From 346664634a0409bb200ca4d321290881b3162b3e Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Wed, 8 May 2024 12:52:21 -0400 Subject: [PATCH 3/4] changelog addition --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4db85572..b9dfef09a 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) #### Fixed From 84ef15b8dcf11fc64afb2915d6c687a8aa84404d Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Wed, 8 May 2024 12:53:55 -0400 Subject: [PATCH 4/4] project readme fixed with NGO version bump --- Basic/ClientDriven/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 [![UnityVersion](https://img.shields.io/badge/Unity%20Version:-2022.3%20LTS-57b9d3.svg?logo=unity&color=2196F3)](https://unity.com/releases/editor/whats-new/2022.3.0) -[![NetcodeVersion](https://img.shields.io/badge/Netcode%20Version:-1.7.1-57b9d3.svg?logo=unity&color=2196F3)](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases/tag/ngo%2F1.7.1) +[![NetcodeVersion](https://img.shields.io/badge/Netcode%20Version:-1.8.1-57b9d3.svg?logo=unity&color=2196F3)](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases/tag/ngo%2F1.8.1) [![LatestRelease](https://img.shields.io/badge/Latest%20%20Github%20Release:-v1.5.0-57b9d3.svg?logo=github&color=brightgreen)](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize/releases/tag/v1.5.0)