Skip to content

Commit e8fa6ed

Browse files
Upgrade Multiplayer Use cases to NGO 1.8.1 [MTT-8499] (#178)
* upgrade ngo to 1.8.1 and related rpc adjustments * included OnConnectionEvent into GameUI.cs script * adjusted scripts to address comments * fixing PascalCase in Rpcs, removing empty Line and naming refactor change version numbers in readme file * remove redundant code in ColorManager
1 parent a081eb3 commit e8fa6ed

File tree

9 files changed

+40
-39
lines changed

9 files changed

+40
-39
lines changed

Experimental/MultiplayerUseCases/Assets/Common/Scripts/GameUI.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,43 @@ public class GameUI : MonoBehaviour
1313

1414
void Start()
1515
{
16-
Refreshlabels(NetworkManager.Singleton && (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer));
17-
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback;
16+
RefreshLabels(NetworkManager.Singleton && (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer));
17+
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
1818
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
19-
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
2019
}
2120

2221
void OnDestroy()
2322
{
2423
if (NetworkManager.Singleton)
2524
{
26-
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
25+
NetworkManager.Singleton.OnConnectionEvent -= OnConnectionEvent;
2726
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
28-
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
2927
}
3028
}
3129

3230
void OnServerStarted()
3331
{
34-
Refreshlabels(true);
32+
RefreshLabels(true);
3533
}
3634

37-
void OnClientConnectedCallback(ulong obj)
35+
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
3836
{
39-
if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer)
37+
if (connectionEventData.EventType == ConnectionEvent.ClientConnected)
4038
{
41-
return; //you don't want to do actions twice when playing as a host
39+
if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer)
40+
{
41+
return; //you don't want to do actions twice when playing as a host
42+
}
43+
RefreshLabels(true);
4244
}
43-
Refreshlabels(true);
44-
}
4545

46-
void OnClientDisconnectCallback(ulong obj)
47-
{
48-
Refreshlabels(false);
46+
else if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected)
47+
{
48+
RefreshLabels(false);
49+
}
4950
}
5051

51-
void Refreshlabels(bool isConnected)
52+
void RefreshLabels(bool isConnected)
5253
{
5354
startupLabel.gameObject.SetActive(!isConnected);
5455
controlsLabel.gameObject.SetActive(isConnected);

Experimental/MultiplayerUseCases/Assets/UseCaseSamples/NetvarVsRpc/Scripts/ColorManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Unity.Netcode.Samples.MultiplayerUseCases.Common;
22
using UnityEngine;
33

4-
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRPC
4+
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRpc
55
{
66
/// <summary>
77
/// Manages the color of a Networked object
@@ -65,23 +65,23 @@ void Update()
6565

6666
void OnClientRequestColorChange()
6767
{
68-
OnServerChangeColorServerRpc();
68+
ServerChangeColorRpc();
6969
}
7070

71-
[ServerRpc(RequireOwnership = false)] //note: please refer to RPCs documentation to learn more about the pros and cons of the RequireOwnership parameter
72-
void OnServerChangeColorServerRpc()
71+
[Rpc(SendTo.Server)]
72+
void ServerChangeColorRpc()
7373
{
7474
Color32 newColor = MultiplayerUseCasesUtilities.GetRandomColor();
7575
if (m_UseNetworkVariableForColor)
7676
{
7777
m_NetworkedColor.Value = newColor;
7878
return;
7979
}
80-
OnClientNotifyColorChangedClientRpc(newColor);
80+
ClientNotifyColorChangedRpc(newColor);
8181
}
8282

83-
[ClientRpc]
84-
void OnClientNotifyColorChangedClientRpc(Color32 newColor)
83+
[Rpc(SendTo.ClientsAndHost)]
84+
void ClientNotifyColorChangedRpc(Color32 newColor)
8585
{
8686
m_Material.color = newColor;
8787
}

Experimental/MultiplayerUseCases/Assets/UseCaseSamples/NetvarVsRpc/Scripts/ProximityTrigger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Unity.Netcode.Samples.MultiplayerUseCases.Common;
22
using UnityEngine;
33

4-
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRPC
4+
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRpc
55
{
66
/// <summary>
77
/// Toggles an object when the local player is close enough

Experimental/MultiplayerUseCases/Assets/UseCaseSamples/NetworkVariables/Scripts/CharacterManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override void OnNetworkSpawn()
4444
if (IsClient)
4545
{
4646
/*
47-
* We call the color change method manually when we connect to ensure that our color is correctly initialized.
47+
* We call the color change method manually when we connect to ensure that our color is correctly initialized.
4848
* This is helpful for when a client joins mid-game and needs to catch up with the current state of the game.
4949
*/
5050
OnClientCustomDataChanged(m_SyncedCustomData.Value, m_SyncedCustomData.Value);
@@ -72,7 +72,7 @@ void Update()
7272
if (!IsServer)
7373
{
7474
/*
75-
* By default, only the server is allowed to change the value of NetworkVariables.
75+
* By default, only the server is allowed to change the value of NetworkVariables.
7676
* This can be changed through the NetworkVariable's constructor.
7777
*/
7878
return;
@@ -101,7 +101,7 @@ void OnClientHealthChanged(int previousHealth, int newHealth)
101101
{
102102
m_HealthBarImage.rectTransform.localScale = new Vector3((float)newHealth / 100.0f, 1);//(float)newHealth / 100.0f;
103103
OnClientUpdateHealthBarColor(newHealth);
104-
//note: you could use the previousHealth to play an healing/damage animation
104+
//note: you could use the previousHealth to play a healing/damage animation
105105
}
106106

107107
void OnClientUpdateHealthBarColor(int newHealth)

Experimental/MultiplayerUseCases/Assets/UseCaseSamples/ProximityChecks/Scripts/ColorManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ void Update()
5959

6060
void OnClientRequestColorChange()
6161
{
62-
OnServerChangeColorServerRpc();
62+
ServerChangeColorRpc();
6363
}
6464

65-
[ServerRpc(RequireOwnership = false)]
66-
void OnServerChangeColorServerRpc()
65+
[Rpc(SendTo.Server)]
66+
void ServerChangeColorRpc()
6767
{
6868
m_NetworkedColor.Value = MultiplayerUseCasesUtilities.GetRandomColor();
6969
}

Experimental/MultiplayerUseCases/Assets/UseCaseSamples/RPCs/Scripts/MoodManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,27 @@ void Update()
3636
if (m_ElapsedSecondsSinceLastChange >= m_SecondsBetweenDataChanges)
3737
{
3838
m_ElapsedSecondsSinceLastChange = 0;
39-
OnServerMoodMessageReceivedServerRpc(s_ChatMessages[Random.Range(0, s_ChatMessages.Length)]);
39+
ServerMoodMessageReceivedRpc(s_ChatMessages[Random.Range(0, s_ChatMessages.Length)]);
4040
}
4141
}
4242

43-
[ServerRpc]
44-
void OnServerMoodMessageReceivedServerRpc(string message)
43+
[Rpc(SendTo.Server)]
44+
void ServerMoodMessageReceivedRpc(string message)
4545
{
4646
/* Here's an example of the type of operation you could do on the server to prevent malicious actions
4747
* from bad actors.
4848
*/
4949
string redactedMessage = OnServerFilterBadWords(message);
50-
OnClientMoodMessageReceivedClientRpc(redactedMessage);
50+
ClientMoodMessageReceivedRpc(redactedMessage);
5151
}
5252

5353
string OnServerFilterBadWords(string message)
5454
{
5555
return MultiplayerUseCasesUtilities.FilterBadWords(message);
5656
}
5757

58-
[ClientRpc]
59-
void OnClientMoodMessageReceivedClientRpc(string message)
58+
[Rpc(SendTo.ClientsAndHost)]
59+
void ClientMoodMessageReceivedRpc(string message)
6060
{
6161
if (!m_SpeechBubble)
6262
{

Experimental/MultiplayerUseCases/Packages/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"com.unity.learn.iet-framework.authoring": "1.2.2",
1111
"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",
1212
"com.unity.multiplayer.tools": "1.1.1",
13-
"com.unity.netcode.gameobjects": "1.7.1",
13+
"com.unity.netcode.gameobjects": "1.8.1",
1414
"com.unity.render-pipelines.universal": "14.0.11",
1515
"com.unity.test-framework": "1.1.33",
1616
"com.unity.textmeshpro": "3.0.6",

Experimental/MultiplayerUseCases/Packages/packages-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
"url": "https://packages.unity.com"
144144
},
145145
"com.unity.netcode.gameobjects": {
146-
"version": "1.7.1",
146+
"version": "1.8.1",
147147
"depth": 0,
148148
"source": "registry",
149149
"dependencies": {

Experimental/MultiplayerUseCases/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Multiplayer Use Cases
22

3-
[![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)
4-
[![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)
3+
[![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.27)
4+
[![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)
55
<br><br>
66

77
This sample provides a series of scenes. Each scene explains a specific API or system commonly used in Netcode for GameObjects. In this sample you will learn more about client-server communication, state synchronization, and other typical mechanics of multiplayer games.

0 commit comments

Comments
 (0)