From 157ba59dc4097e43e462a82d1cb35473045338e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elfi=20K=C3=BChndorf?= Date: Tue, 7 May 2024 14:25:15 +0200 Subject: [PATCH 1/5] upgrading to NGO 1.8.1 and adjusting scripts to include new RPC attribute and OnConnectionEvent --- Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs | 6 ++-- .../Assets/Scripts/NetworkManagerHud.cs | 29 ++++++++++--------- .../Assets/Scripts/ShipControl.cs | 28 +++++++++--------- Basic/2DSpaceShooter/Packages/manifest.json | 2 +- .../Packages/packages-lock.json | 2 +- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs b/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs index 2176f5062..53ca86cbe 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs @@ -52,12 +52,12 @@ public void SetVelocity(Vector2 velocity) { var bulletRb = GetComponent(); bulletRb.velocity = velocity; - SetVelocityClientRpc(velocity); + ClientSetVelocityRpc(velocity); } } - [ClientRpc] - void SetVelocityClientRpc(Vector2 velocity) + [Rpc(SendTo.ClientsAndHost)] + void ClientSetVelocityRpc(Vector2 velocity) { if (!IsHost) { diff --git a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs index 9b56af396..a9580e7e1 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs @@ -80,24 +80,26 @@ void Start() ShowInGameUI(false); ShowStatusText(false); - NetworkManager.Singleton.OnClientConnectedCallback += OnOnClientConnectedCallback; - NetworkManager.Singleton.OnClientDisconnectCallback += OnOnClientDisconnectCallback; + NetworkManager.Singleton.OnConnectionEvent += SingletonOnOnConnectionEvent; } - void OnOnClientConnectedCallback(ulong obj) + void SingletonOnOnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData) { - ShowMainMenuUI(false); - ShowInGameUI(true); - } + if (connectionEventData.EventType == ConnectionEvent.ClientConnected) + { + ShowMainMenuUI(false); + ShowInGameUI(true); + } - void OnOnClientDisconnectCallback(ulong clientId) - { - if ((NetworkManager.Singleton.IsServer && clientId != NetworkManager.ServerClientId)) + else if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected) { - return; + if ((NetworkManager.Singleton.IsServer && connectionEventData.ClientId != NetworkManager.ServerClientId)) + { + return; + } + ShowMainMenuUI(true); + ShowInGameUI(false); } - ShowMainMenuUI(true); - ShowInGameUI(false); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -262,7 +264,6 @@ void OnDestroy() { m_ShutdownButton.clickable.clickedWithEventInfo -= ShutdownButtonClicked; } - m_NetworkManager.OnClientConnectedCallback -= OnOnClientConnectedCallback; - m_NetworkManager.OnClientDisconnectCallback -= OnOnClientDisconnectCallback; + m_NetworkManager.OnConnectionEvent -= SingletonOnOnConnectionEvent; } } diff --git a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs index a176c9bef..2fe7d7816 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs @@ -67,7 +67,7 @@ public class ShipControl : NetworkBehaviour bool m_IsBuffed; - public NetworkVariable PlayerName = new NetworkVariable(new FixedString32Bytes("")); + public NetworkVariable PlayerName = new NetworkVariable(new FixedString32Bytes()); [SerializeField] ParticleSystem m_Friction; @@ -141,8 +141,8 @@ public override void OnNetworkSpawn() if (IsServer) { LatestShipColor.Value = m_ShipGlowDefaultColor; - - PlayerName.Value = $"Player {OwnerClientId}"; + var playerNameString = PlayerName.Value.ToString(); + playerNameString = $"Player {OwnerClientId}"; if (!IsHost) { @@ -199,7 +199,7 @@ public void TakeDamage(int amount) void Fire(Vector3 direction) { - PlayFireSoundClientRpc(); + ClientPlayFireSoundRpc(); var damage = 5; if (QuadDamageTimer.Value > NetworkManager.ServerTime.TimeAsFloat) { @@ -366,7 +366,7 @@ void UpdateClient() if (m_OldMoveForce != moveForce || m_OldSpin != spin) { - ThrustServerRpc(moveForce, spin); + ServerThrustRpc(moveForce, spin); m_OldMoveForce = moveForce; m_OldSpin = spin; } @@ -388,7 +388,7 @@ void UpdateClient() // fire if (Input.GetKeyDown(KeyCode.Space)) { - FireServerRpc(); + ServerFireRpc(); } } @@ -505,22 +505,22 @@ void OnCollisionEnter2D(Collision2D other) // --- ClientRPCs --- - [ClientRpc] - void PlayFireSoundClientRpc() + [Rpc(SendTo.ClientsAndHost)] + void ClientPlayFireSoundRpc() { fireSound.Play(); } // --- ServerRPCs --- - [ServerRpc] - public void ThrustServerRpc(float thrusting, int spin) + [Rpc(SendTo.Server)] + public void ServerThrustRpc(float thrusting, int spin) { m_Thrust = thrusting; m_Spin = spin; } - [ServerRpc] - public void FireServerRpc() + [Rpc(SendTo.Server)] + public void ServerFireRpc() { if (Energy.Value >= 10) { @@ -549,8 +549,8 @@ public void FireServerRpc() } } - [ServerRpc] - public void SetNameServerRpc(string name) + [Rpc(SendTo.Server)] + public void ServerSetNameRpc(FixedString32Bytes name) { PlayerName.Value = name; } diff --git a/Basic/2DSpaceShooter/Packages/manifest.json b/Basic/2DSpaceShooter/Packages/manifest.json index bde354535..931873e68 100644 --- a/Basic/2DSpaceShooter/Packages/manifest.json +++ b/Basic/2DSpaceShooter/Packages/manifest.json @@ -7,7 +7,7 @@ "com.unity.ide.visualstudio": "2.0.22", "com.unity.ide.vscode": "1.2.5", "com.unity.multiplayer.samples.coop": "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git?path=/Packages/com.unity.multiplayer.samples.coop#v2.4.0", - "com.unity.netcode.gameobjects": "1.7.1", + "com.unity.netcode.gameobjects": "1.8.1", "com.unity.postprocessing": "3.2.2", "com.unity.render-pipelines.universal": "14.0.9", "com.unity.test-framework": "1.1.33", diff --git a/Basic/2DSpaceShooter/Packages/packages-lock.json b/Basic/2DSpaceShooter/Packages/packages-lock.json index d184065ee..9e56200c3 100644 --- a/Basic/2DSpaceShooter/Packages/packages-lock.json +++ b/Basic/2DSpaceShooter/Packages/packages-lock.json @@ -123,7 +123,7 @@ "url": "https://packages.unity.com" }, "com.unity.netcode.gameobjects": { - "version": "1.7.1", + "version": "1.8.1", "depth": 0, "source": "registry", "dependencies": { From 9c64ec6c68d906e82030f4baf44e1941abd104bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elfi=20K=C3=BChndorf?= Date: Thu, 9 May 2024 14:47:41 +0200 Subject: [PATCH 2/5] update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4db85572..f83b4014a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,9 @@ - upgraded to com.unity.services.qos v1.3.0 - upgraded to com.unity.transport v1.4.1 - upgraded to com.unity.services.core v1.12.5 +- Upgraded to Netcode for GameObjects v1.8.1 (#174) + - Upgraded to the newer API for Rpcs, Universal Rpcs + - Upgraded to newer API for Connection Events, OnConnectionEvent #### Fixed - Reset values and buffs after respawn of ship (#167) From 40a2136f5e03c5ca7fba717fef6344a1fc74a82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elfi=20K=C3=BChndorf?= Date: Mon, 13 May 2024 10:43:08 +0200 Subject: [PATCH 3/5] rename of OnConnectionEvent --- Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs index a9580e7e1..dee5c6e95 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs @@ -80,10 +80,10 @@ void Start() ShowInGameUI(false); ShowStatusText(false); - NetworkManager.Singleton.OnConnectionEvent += SingletonOnOnConnectionEvent; + NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent; } - void SingletonOnOnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData) + void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData) { if (connectionEventData.EventType == ConnectionEvent.ClientConnected) { @@ -264,6 +264,6 @@ void OnDestroy() { m_ShutdownButton.clickable.clickedWithEventInfo -= ShutdownButtonClicked; } - m_NetworkManager.OnConnectionEvent -= SingletonOnOnConnectionEvent; + m_NetworkManager.OnConnectionEvent -= OnConnectionEvent; } } From d6c5bd3d0aabf12d13a2182501c6c1458104ce3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elfi=20K=C3=BChndorf?= Date: Tue, 14 May 2024 10:39:07 +0200 Subject: [PATCH 4/5] revert string changes --- Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs index 2fe7d7816..c1a22d4f6 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs @@ -67,7 +67,7 @@ public class ShipControl : NetworkBehaviour bool m_IsBuffed; - public NetworkVariable PlayerName = new NetworkVariable(new FixedString32Bytes()); + public NetworkVariable PlayerName = new NetworkVariable(new FixedString32Bytes("")); [SerializeField] ParticleSystem m_Friction; @@ -141,8 +141,7 @@ public override void OnNetworkSpawn() if (IsServer) { LatestShipColor.Value = m_ShipGlowDefaultColor; - var playerNameString = PlayerName.Value.ToString(); - playerNameString = $"Player {OwnerClientId}"; + PlayerName.Value = $"Player {OwnerClientId}"; if (!IsHost) { From 3c7d4da16b7d8aa5564792238e314d6806f06ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elfi=20K=C3=BChndorf?= Date: Wed, 15 May 2024 14:20:35 +0200 Subject: [PATCH 5/5] remove empty line --- Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs index dee5c6e95..8f2c373ff 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs @@ -90,7 +90,6 @@ void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connec ShowMainMenuUI(false); ShowInGameUI(true); } - else if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected) { if ((NetworkManager.Singleton.IsServer && connectionEventData.ClientId != NetworkManager.ServerClientId))