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
This commit is contained in:
Fernando Cortez 2024-05-13 15:07:22 -04:00 коммит произвёл GitHub
Родитель 634b8467ba
Коммит f2e33e9e66
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 21 добавлений и 15 удалений

Просмотреть файл

@ -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:

Просмотреть файл

@ -9,7 +9,7 @@ namespace Unity.Multiplayer.Samples.ClientDriven
/// <remarks>
/// A NetworkManager is expected to be part of the scene that this NetworkObject is a part of.
/// </remarks>
internal class NetworkObjectSpawner : MonoBehaviour
class NetworkObjectSpawner : MonoBehaviour
{
[SerializeField]
NetworkObject m_PrefabReference;
@ -27,17 +27,20 @@ namespace Unity.Multiplayer.Samples.ClientDriven
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<ServerIngredient>();
ingredient.NetworkObject.Spawn();
instantiatedNetworkObject.Spawn();
}
}
}

Просмотреть файл

@ -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 class ServerIngredientSpawner : NetworkBehaviour
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 @@ public class ServerIngredientSpawner : NetworkBehaviour
{
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<ServerIngredient>();
ingredient.NetworkObject.Spawn();
ingredient.currentIngredientType.Value = (IngredientType)m_RandomGenerator.Next((int)IngredientType.MAX);
}
m_SpawnWaves++;

Просмотреть файл

@ -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)
<br><br>

Просмотреть файл

@ -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