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
This commit is contained in:
Elfi0Kuhndorf 2024-05-28 16:43:45 +02:00 коммит произвёл GitHub
Родитель a081eb3ba0
Коммит e8fa6ed223
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
9 изменённых файлов: 41 добавлений и 40 удалений

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

@ -13,42 +13,43 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.Common
void Start()
{
Refreshlabels(NetworkManager.Singleton && (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer));
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback;
RefreshLabels(NetworkManager.Singleton && (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer));
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
}
void OnDestroy()
{
if (NetworkManager.Singleton)
{
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
NetworkManager.Singleton.OnConnectionEvent -= OnConnectionEvent;
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
}
}
void OnServerStarted()
{
Refreshlabels(true);
RefreshLabels(true);
}
void OnClientConnectedCallback(ulong obj)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer)
if (connectionEventData.EventType == ConnectionEvent.ClientConnected)
{
return; //you don't want to do actions twice when playing as a host
if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer)
{
return; //you don't want to do actions twice when playing as a host
}
RefreshLabels(true);
}
else if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected)
{
RefreshLabels(false);
}
Refreshlabels(true);
}
void OnClientDisconnectCallback(ulong obj)
{
Refreshlabels(false);
}
void Refreshlabels(bool isConnected)
void RefreshLabels(bool isConnected)
{
startupLabel.gameObject.SetActive(!isConnected);
controlsLabel.gameObject.SetActive(isConnected);

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

@ -1,7 +1,7 @@
using Unity.Netcode.Samples.MultiplayerUseCases.Common;
using UnityEngine;
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRPC
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRpc
{
/// <summary>
/// Manages the color of a Networked object
@ -65,11 +65,11 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRPC
void OnClientRequestColorChange()
{
OnServerChangeColorServerRpc();
ServerChangeColorRpc();
}
[ServerRpc(RequireOwnership = false)] //note: please refer to RPCs documentation to learn more about the pros and cons of the RequireOwnership parameter
void OnServerChangeColorServerRpc()
[Rpc(SendTo.Server)]
void ServerChangeColorRpc()
{
Color32 newColor = MultiplayerUseCasesUtilities.GetRandomColor();
if (m_UseNetworkVariableForColor)
@ -77,11 +77,11 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRPC
m_NetworkedColor.Value = newColor;
return;
}
OnClientNotifyColorChangedClientRpc(newColor);
ClientNotifyColorChangedRpc(newColor);
}
[ClientRpc]
void OnClientNotifyColorChangedClientRpc(Color32 newColor)
[Rpc(SendTo.ClientsAndHost)]
void ClientNotifyColorChangedRpc(Color32 newColor)
{
m_Material.color = newColor;
}

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

@ -1,7 +1,7 @@
using Unity.Netcode.Samples.MultiplayerUseCases.Common;
using UnityEngine;
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRPC
namespace Unity.Netcode.Samples.MultiplayerUseCases.NetVarVsRpc
{
/// <summary>
/// Toggles an object when the local player is close enough

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

@ -44,7 +44,7 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.NetworkVariables
if (IsClient)
{
/*
* We call the color change method manually when we connect to ensure that our color is correctly initialized.
* We call the color change method manually when we connect to ensure that our color is correctly initialized.
* This is helpful for when a client joins mid-game and needs to catch up with the current state of the game.
*/
OnClientCustomDataChanged(m_SyncedCustomData.Value, m_SyncedCustomData.Value);
@ -72,7 +72,7 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.NetworkVariables
if (!IsServer)
{
/*
* By default, only the server is allowed to change the value of NetworkVariables.
* By default, only the server is allowed to change the value of NetworkVariables.
* This can be changed through the NetworkVariable's constructor.
*/
return;
@ -101,7 +101,7 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.NetworkVariables
{
m_HealthBarImage.rectTransform.localScale = new Vector3((float)newHealth / 100.0f, 1);//(float)newHealth / 100.0f;
OnClientUpdateHealthBarColor(newHealth);
//note: you could use the previousHealth to play an healing/damage animation
//note: you could use the previousHealth to play a healing/damage animation
}
void OnClientUpdateHealthBarColor(int newHealth)

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

@ -59,11 +59,11 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.Proximity
void OnClientRequestColorChange()
{
OnServerChangeColorServerRpc();
ServerChangeColorRpc();
}
[ServerRpc(RequireOwnership = false)]
void OnServerChangeColorServerRpc()
[Rpc(SendTo.Server)]
void ServerChangeColorRpc()
{
m_NetworkedColor.Value = MultiplayerUseCasesUtilities.GetRandomColor();
}

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

@ -36,18 +36,18 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.RPC
if (m_ElapsedSecondsSinceLastChange >= m_SecondsBetweenDataChanges)
{
m_ElapsedSecondsSinceLastChange = 0;
OnServerMoodMessageReceivedServerRpc(s_ChatMessages[Random.Range(0, s_ChatMessages.Length)]);
ServerMoodMessageReceivedRpc(s_ChatMessages[Random.Range(0, s_ChatMessages.Length)]);
}
}
[ServerRpc]
void OnServerMoodMessageReceivedServerRpc(string message)
[Rpc(SendTo.Server)]
void ServerMoodMessageReceivedRpc(string message)
{
/* Here's an example of the type of operation you could do on the server to prevent malicious actions
* from bad actors.
*/
string redactedMessage = OnServerFilterBadWords(message);
OnClientMoodMessageReceivedClientRpc(redactedMessage);
ClientMoodMessageReceivedRpc(redactedMessage);
}
string OnServerFilterBadWords(string message)
@ -55,8 +55,8 @@ namespace Unity.Netcode.Samples.MultiplayerUseCases.RPC
return MultiplayerUseCasesUtilities.FilterBadWords(message);
}
[ClientRpc]
void OnClientMoodMessageReceivedClientRpc(string message)
[Rpc(SendTo.ClientsAndHost)]
void ClientMoodMessageReceivedRpc(string message)
{
if (!m_SpeechBubble)
{

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

@ -10,7 +10,7 @@
"com.unity.learn.iet-framework.authoring": "1.2.2",
"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.multiplayer.tools": "1.1.1",
"com.unity.netcode.gameobjects": "1.7.1",
"com.unity.netcode.gameobjects": "1.8.1",
"com.unity.render-pipelines.universal": "14.0.11",
"com.unity.test-framework": "1.1.33",
"com.unity.textmeshpro": "3.0.6",

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

@ -143,7 +143,7 @@
"url": "https://packages.unity.com"
},
"com.unity.netcode.gameobjects": {
"version": "1.7.1",
"version": "1.8.1",
"depth": 0,
"source": "registry",
"dependencies": {

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

@ -1,7 +1,7 @@
# Multiplayer Use Cases
[![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)
[![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)
[![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)
<br><br>
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.