Skip to content

Commit f2e33e9

Browse files
feat: ClientDriven further Netcode for GameObejcts v1.8.1 API upgrades [MTT-8543] (#173)
* using NetworkObject.InstantiateAndSpawn() where possible * further clarifying InstantiateAndSpawn() issue * changelog addition * project readme fixed with NGO version bump
1 parent 634b846 commit f2e33e9

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

Basic/ClientDriven/Assets/Scenes/Bootstrap.unity

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.21890283, g: 0.16790575, b: 0.67034173, a: 1}
41+
m_IndirectSpecularColor: {r: 0.21890238, g: 0.16790517, b: 0.6703396, a: 1}
4242
m_UseRadianceAmbientProbe: 0
4343
--- !u!157 &3
4444
LightmapSettings:
@@ -925,6 +925,7 @@ MonoBehaviour:
925925
m_Name:
926926
m_EditorClassIdentifier:
927927
GlobalObjectIdHash: 2084257867
928+
InScenePlacedSourceGlobalObjectIdHash: 0
928929
AlwaysReplicateAsRoot: 0
929930
SynchronizeTransform: 1
930931
ActiveSceneSynchronization: 0
@@ -1525,6 +1526,7 @@ MonoBehaviour:
15251526
m_Name:
15261527
m_EditorClassIdentifier:
15271528
GlobalObjectIdHash: 1374255023
1529+
InScenePlacedSourceGlobalObjectIdHash: 0
15281530
AlwaysReplicateAsRoot: 0
15291531
SynchronizeTransform: 1
15301532
ActiveSceneSynchronization: 0
@@ -1550,7 +1552,7 @@ MonoBehaviour:
15501552
- {fileID: 1422708175}
15511553
- {fileID: 796975752}
15521554
m_SpawnRatePerSecond: 10
1553-
m_IngredientPrefab: {fileID: 8321201880322001125, guid: a6b33b41508134c09957e076f4d53415, type: 3}
1555+
m_IngredientPrefab: {fileID: 5818429371130516787, guid: a6b33b41508134c09957e076f4d53415, type: 3}
15541556
m_MaxSpawnWaves: 8
15551557
--- !u!4 &2060465724
15561558
Transform:

Basic/ClientDriven/Assets/Scripts/NetworkObjectSpawner.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Unity.Multiplayer.Samples.ClientDriven
99
/// <remarks>
1010
/// A NetworkManager is expected to be part of the scene that this NetworkObject is a part of.
1111
/// </remarks>
12-
internal class NetworkObjectSpawner : MonoBehaviour
12+
class NetworkObjectSpawner : MonoBehaviour
1313
{
1414
[SerializeField]
1515
NetworkObject m_PrefabReference;
@@ -27,17 +27,20 @@ void Start()
2727

2828
void OnDestroy()
2929
{
30-
if(NetworkManager.Singleton != null)
31-
{
30+
if (NetworkManager.Singleton != null)
31+
{
3232
NetworkManager.Singleton.OnServerStarted -= SpawnIngredient;
3333
}
3434
}
3535

3636
void SpawnIngredient()
3737
{
38+
// Note: this will be converted to NetworkObject.InstantiateAndSpawn(), but a current limitation on Netcode
39+
// for GameObjects invoking this method on OnServerStarted prevents this API upgrade.
40+
// Specifically, if you were to spawn a Rigidbody with Rigidbody Interpolation enabled, you would need to
41+
// update the Rigidbody's position immediately after invoking NetworkObject.InstantitateAndSpawn().
3842
NetworkObject instantiatedNetworkObject = Instantiate(m_PrefabReference, transform.position, transform.rotation, null);
39-
var ingredient = instantiatedNetworkObject.GetComponent<ServerIngredient>();
40-
ingredient.NetworkObject.Spawn();
43+
instantiatedNetworkObject.Spawn();
4144
}
4245
}
4346
}

Basic/ClientDriven/Assets/Scripts/ServerIngredientSpawner.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ServerIngredientSpawner : NetworkBehaviour
1212
float m_SpawnRatePerSecond;
1313

1414
[SerializeField]
15-
GameObject m_IngredientPrefab;
15+
NetworkObject m_IngredientPrefab;
1616

1717
[SerializeField]
1818
int m_MaxSpawnWaves;
@@ -26,9 +26,9 @@ public class ServerIngredientSpawner : NetworkBehaviour
2626
public override void OnNetworkSpawn()
2727
{
2828
base.OnNetworkSpawn();
29+
enabled = IsServer;
2930
if (!IsServer)
3031
{
31-
enabled = false;
3232
return;
3333
}
3434

@@ -38,11 +38,12 @@ public override void OnNetworkSpawn()
3838
public override void OnNetworkDespawn()
3939
{
4040
m_SpawnWaves = 0;
41+
enabled = false;
4142
}
4243

4344
void FixedUpdate()
4445
{
45-
if (NetworkManager != null && !IsServer)
46+
if (NetworkManager == null || !NetworkManager.IsListening || !IsServer)
4647
{
4748
return;
4849
}
@@ -51,11 +52,10 @@ void FixedUpdate()
5152
{
5253
foreach (var spawnPoint in m_SpawnPoints)
5354
{
54-
var newIngredientObject = Instantiate(m_IngredientPrefab, spawnPoint.transform.position, spawnPoint.transform.rotation);
55-
newIngredientObject.transform.position = spawnPoint.transform.position +
56-
new Vector3(UnityEngine.Random.Range(-0.25f, 0.25f), 0, UnityEngine.Random.Range(-0.25f, 0.25f));
55+
var newIngredientObject = m_IngredientPrefab.InstantiateAndSpawn(NetworkManager,
56+
position: spawnPoint.transform.position + new Vector3(UnityEngine.Random.Range(-0.25f, 0.25f), 0, UnityEngine.Random.Range(-0.25f, 0.25f)),
57+
rotation: spawnPoint.transform.rotation);
5758
var ingredient = newIngredientObject.GetComponent<ServerIngredient>();
58-
ingredient.NetworkObject.Spawn();
5959
ingredient.currentIngredientType.Value = (IngredientType)m_RandomGenerator.Next((int)IngredientType.MAX);
6060
}
6161
m_SpawnWaves++;

Basic/ClientDriven/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Client Driven
55

66
[![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)
7-
[![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)
7+
[![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)
88
[![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)
99
<br><br>
1010

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Upgraded to Netcode for GameObjects v1.8.1 (#164)
3535
- Upgraded to the newer API for Rpcs, Universal Rpcs
3636
- 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
37+
- Upgraded to use NetworkObject.InstantiateAndSpawn() API where appropriate (#173)
3738
- Upgraded to IDE Rider v3.0.28 (#166)
3839
- Upgraded to Unity 2022.3.27f1 (#175)
3940
- com.unity.render-pipelines.core upgraded to v14.0.11

0 commit comments

Comments
 (0)