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..8f2c373ff 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs @@ -80,24 +80,25 @@ void Start() ShowInGameUI(false); ShowStatusText(false); - NetworkManager.Singleton.OnClientConnectedCallback += OnOnClientConnectedCallback; - NetworkManager.Singleton.OnClientDisconnectCallback += OnOnClientDisconnectCallback; + NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent; } - void OnOnClientConnectedCallback(ulong obj) + void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData) { - ShowMainMenuUI(false); - ShowInGameUI(true); - } - - void OnOnClientDisconnectCallback(ulong clientId) - { - if ((NetworkManager.Singleton.IsServer && clientId != NetworkManager.ServerClientId)) + if (connectionEventData.EventType == ConnectionEvent.ClientConnected) { - return; + ShowMainMenuUI(false); + ShowInGameUI(true); + } + else if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected) + { + if ((NetworkManager.Singleton.IsServer && connectionEventData.ClientId != NetworkManager.ServerClientId)) + { + return; + } + ShowMainMenuUI(true); + ShowInGameUI(false); } - ShowMainMenuUI(true); - ShowInGameUI(false); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -262,7 +263,6 @@ void OnDestroy() { m_ShutdownButton.clickable.clickedWithEventInfo -= ShutdownButtonClicked; } - m_NetworkManager.OnClientConnectedCallback -= OnOnClientConnectedCallback; - m_NetworkManager.OnClientDisconnectCallback -= OnOnClientDisconnectCallback; + m_NetworkManager.OnConnectionEvent -= OnConnectionEvent; } } diff --git a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs index a176c9bef..c1a22d4f6 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs @@ -141,7 +141,6 @@ public override void OnNetworkSpawn() if (IsServer) { LatestShipColor.Value = m_ShipGlowDefaultColor; - PlayerName.Value = $"Player {OwnerClientId}"; if (!IsHost) @@ -199,7 +198,7 @@ public void TakeDamage(int amount) void Fire(Vector3 direction) { - PlayFireSoundClientRpc(); + ClientPlayFireSoundRpc(); var damage = 5; if (QuadDamageTimer.Value > NetworkManager.ServerTime.TimeAsFloat) { @@ -366,7 +365,7 @@ void UpdateClient() if (m_OldMoveForce != moveForce || m_OldSpin != spin) { - ThrustServerRpc(moveForce, spin); + ServerThrustRpc(moveForce, spin); m_OldMoveForce = moveForce; m_OldSpin = spin; } @@ -388,7 +387,7 @@ void UpdateClient() // fire if (Input.GetKeyDown(KeyCode.Space)) { - FireServerRpc(); + ServerFireRpc(); } } @@ -505,22 +504,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 +548,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 31933c71a..f26807263 100644 --- a/Basic/2DSpaceShooter/Packages/packages-lock.json +++ b/Basic/2DSpaceShooter/Packages/packages-lock.json @@ -124,7 +124,7 @@ "url": "https://packages.unity.com" }, "com.unity.netcode.gameobjects": { - "version": "1.7.1", + "version": "1.8.1", "depth": 0, "source": "registry", "dependencies": { diff --git a/CHANGELOG.md b/CHANGELOG.md index b732a47df..e203ef55f 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)