Skip to content

feat: OnConnectionEvent callback usage in project and Utilities package [MTTB-1147] #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions Assets/Scripts/ConnectionManagement/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ void Start()

m_CurrentState = m_Offline;

NetworkManager.OnClientConnectedCallback += OnClientConnectedCallback;
NetworkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
NetworkManager.OnConnectionEvent += OnConnectionEvent;
NetworkManager.OnServerStarted += OnServerStarted;
NetworkManager.ConnectionApprovalCallback += ApprovalCheck;
NetworkManager.OnTransportFailure += OnTransportFailure;
Expand All @@ -103,8 +102,7 @@ void Start()

void OnDestroy()
{
NetworkManager.OnClientConnectedCallback -= OnClientConnectedCallback;
NetworkManager.OnClientDisconnectCallback -= OnClientDisconnectCallback;
NetworkManager.OnConnectionEvent -= OnConnectionEvent;
NetworkManager.OnServerStarted -= OnServerStarted;
NetworkManager.ConnectionApprovalCallback -= ApprovalCheck;
NetworkManager.OnTransportFailure -= OnTransportFailure;
Expand All @@ -123,14 +121,17 @@ internal void ChangeState(ConnectionState nextState)
m_CurrentState.Enter();
}

void OnClientDisconnectCallback(ulong clientId)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
m_CurrentState.OnClientDisconnect(clientId);
}

void OnClientConnectedCallback(ulong clientId)
{
m_CurrentState.OnClientConnected(clientId);
switch (connectionEventData.EventType)
{
case ConnectionEvent.ClientConnected:
m_CurrentState.OnClientConnected(connectionEventData.ClientId);
break;
case ConnectionEvent.ClientDisconnected:
m_CurrentState.OnClientDisconnect(connectionEventData.ClientId);
break;
}
}

void OnServerStarted()
Expand Down
15 changes: 9 additions & 6 deletions Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void OnNetworkSpawn()
m_PersistentGameState.Reset();
m_LifeStateChangedEventMessageSubscriber.Subscribe(OnLifeStateChangedEventMessage);

NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnect;
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += OnLoadEventCompleted;
NetworkManager.Singleton.SceneManager.OnSynchronizeComplete += OnSynchronizeComplete;

Expand All @@ -93,7 +93,7 @@ void OnNetworkDespawn()
m_LifeStateChangedEventMessageSubscriber.Unsubscribe(OnLifeStateChangedEventMessage);
}

NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnect;
NetworkManager.Singleton.OnConnectionEvent -= OnConnectionEvent;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= OnLoadEventCompleted;
NetworkManager.Singleton.SceneManager.OnSynchronizeComplete -= OnSynchronizeComplete;
}
Expand Down Expand Up @@ -138,12 +138,15 @@ void OnLoadEventCompleted(string sceneName, LoadSceneMode loadSceneMode, List<ul
}
}

void OnClientDisconnect(ulong clientId)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
if (clientId != NetworkManager.Singleton.LocalClientId)
if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected)
{
// If a client disconnects, check for game over in case all other players are already down
StartCoroutine(WaitToCheckForGameOver());
if (connectionEventData.ClientId != networkManager.LocalClientId)
{
// If a client disconnects, check for game over in case all other players are already down
StartCoroutine(WaitToCheckForGameOver());
}
}
}

Expand Down
33 changes: 18 additions & 15 deletions Assets/Scripts/Gameplay/GameState/ServerCharSelectState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ IEnumerator WaitToEndSession()
SceneLoaderWrapper.Instance.LoadScene("BossRoom", useNetworkSceneManager: true);
}

public void OnNetworkDespawn()
void OnNetworkDespawn()
{
if (NetworkManager.Singleton)
{
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
NetworkManager.Singleton.OnConnectionEvent -= OnConnectionEvent;
NetworkManager.Singleton.SceneManager.OnSceneEvent -= OnSceneEvent;
}
if (networkCharSelection)
Expand All @@ -197,15 +197,15 @@ public void OnNetworkDespawn()
}
}

public void OnNetworkSpawn()
void OnNetworkSpawn()
{
if (!NetworkManager.Singleton.IsServer)
{
enabled = false;
}
else
{
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
networkCharSelection.OnClientChangedSeat += OnClientChangedSeat;

NetworkManager.Singleton.SceneManager.OnSceneEvent += OnSceneEvent;
Expand Down Expand Up @@ -276,22 +276,25 @@ void SeatNewPlayer(ulong clientId)
}
}

void OnClientDisconnectCallback(ulong clientId)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
// clear this client's PlayerNumber and any associated visuals (so other players know they're gone).
for (int i = 0; i < networkCharSelection.sessionPlayers.Count; ++i)
if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected)
{
if (networkCharSelection.sessionPlayers[i].ClientId == clientId)
// clear this client's PlayerNumber and any associated visuals (so other players know they're gone).
for (int i = 0; i < networkCharSelection.sessionPlayers.Count; ++i)
{
networkCharSelection.sessionPlayers.RemoveAt(i);
break;
if (networkCharSelection.sessionPlayers[i].ClientId == connectionEventData.ClientId)
{
networkCharSelection.sessionPlayers.RemoveAt(i);
break;
}
}
}

if (!networkCharSelection.IsSessionClosed.Value)
{
// If the Session is not already closing, close if the remaining players are all ready
CloseSessionIfReady();
if (!networkCharSelection.IsSessionClosed.Value)
{
// If the Session is not already closing, close if the remaining players are all ready
CloseSessionIfReady();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public NetworkedMessageChannel()
void InjectDependencies(NetworkManager networkManager)
{
m_NetworkManager = networkManager;
m_NetworkManager.OnClientConnectedCallback += OnClientConnected;
m_NetworkManager.OnConnectionEvent += OnConnectionEvent;
if (m_NetworkManager.IsListening)
{
RegisterHandler();
Expand All @@ -40,14 +40,18 @@ public override void Dispose()
if (m_NetworkManager != null && m_NetworkManager.CustomMessagingManager != null)
{
m_NetworkManager.CustomMessagingManager.UnregisterNamedMessageHandler(m_Name);
m_NetworkManager.OnConnectionEvent -= OnConnectionEvent;
}
}
base.Dispose();
}

void OnClientConnected(ulong clientId)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
RegisterHandler();
if (connectionEventData.EventType == ConnectionEvent.ClientConnected)
{
RegisterHandler();
}
}

void RegisterHandler()
Expand Down
38 changes: 20 additions & 18 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@ Additional documentation and release notes are available at [Multiplayer Documen
* VContainer package upgraded from v1.11.0 to v1.14.0 (#896) This upgrade removes warning messages related to IL weaving as code gen is refactored in v1.14.0
* Upgraded the project to the New Input System v1.11.2 (#897) Compatibility mode with the previous version has been disabled.
* Upgraded editor version to 6000.0.42f1 and upgraded packages (#901):
* com.unity.ai.navigation from v2.0.5 to v2.0.6
* com.unity.collab-proxy from v2.6.0 to v2.7.1
* com.unity.inputsystem from v1.11.2 to v1.13.1
* com.unity.learn.iet-framework from v4.0.3 to v4.0.4
* com.unity.memoryprofiler from v1.1.3 to v1.1.5
* com.unity.netcode.gameobjects from v2.1.1 to v2.2.0
* com.unity.render-pipelines-universal from v17.0.3 to v17.0.4
* com.unity.services.multiplayer from v1.1.0 to v1.1.1
* com.unity.test-framework from v1.4.5 to v1.4.6
* com.unity.ai.navigation from v2.0.5 to v2.0.6
* com.unity.collab-proxy from v2.6.0 to v2.7.1
* com.unity.inputsystem from v1.11.2 to v1.13.1
* com.unity.learn.iet-framework from v4.0.3 to v4.0.4
* com.unity.memoryprofiler from v1.1.3 to v1.1.5
* com.unity.netcode.gameobjects from v2.1.1 to v2.2.0
* com.unity.render-pipelines-universal from v17.0.3 to v17.0.4
* com.unity.services.multiplayer from v1.1.0 to v1.1.1
* com.unity.test-framework from v1.4.5 to v1.4.6
* Upgraded editor version to 6000.0.44f1 and upgraded packages (#902):
* com.unity.ide.rider from v3.0.34 to v3.0.35
* com.unity.services.multiplayer from v1.1.1 to v1.1.2
* com.unity.test-framework from v1.4.6 to v1.5.1
* com.unity.transport from v2.4.0 to v2.5.0
* com.unity.cinemachine from v.3.1.2 to 3.1.3
* com.unity.services.authentication from 3.4.0 to 3.4.1
* jp.hadashikick.vcontainer from 1.14.0 to 1.16.8
* com.unity.memoryprofiler from 1.1.5 to 1.1.6
* com.unity.timeline from 1.8.7 to 1.8.8
* com.unity.ide.rider from v3.0.34 to v3.0.35
* com.unity.services.multiplayer from v1.1.1 to v1.1.2
* com.unity.test-framework from v1.4.6 to v1.5.1
* com.unity.transport from v2.4.0 to v2.5.0
* com.unity.cinemachine from v.3.1.2 to 3.1.3
* com.unity.services.authentication from 3.4.0 to 3.4.1
* jp.hadashikick.vcontainer from 1.14.0 to 1.16.8
* com.unity.memoryprofiler from 1.1.5 to 1.1.6
* com.unity.timeline from 1.8.7 to 1.8.8
* Upgraded Unity Transport package to 2.5.1 (#906)
* Upgraded the project to use Netcode for GameObject's recommended OnConnectionEvent callback (#907)

### Cleanup
* Removed ParrelSync from the project (#890)
Expand All @@ -61,6 +62,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
* Fixed a visual issue where a selected party member's party HUD slot would be displayed as selected (green) when the same or new party member joins the session in-game (#899)
* Fixed Render Graph API compatibility mode warning and switched active input handling settings to "both" for removing InvalidOperationException (#901)
* Fixed error logged when unsubscribing from Session events when removed from a Session (#905)
* Fixed error logged when attempting to despawn an already despawned LoadingProgressTracker NetworkObject (#907)

## [2.5.0] - 2024-04-18

Expand Down
9 changes: 5 additions & 4 deletions Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

### Changed
* Replaced Lobby and Relay standalone packages with the Multiplayer Services package v1.0.2 (#892)
* Removed UnityRelayUtilities
* Removed UnityRelayUtilities
* Upgrading Editor version dependency to 6000.0 (#902)
Upgrading package dependencies:
* Tutorial Framework upgraded to v4.0.4
* Multiplayer Tools to v2.2.3
* Netcode for GameObjects to v2.2.0
* Tutorial Framework upgraded to v4.0.4
* Multiplayer Tools to v2.2.3
* Netcode for GameObjects to v2.2.0
* Upgraded Utilities package to use Netcode for GameObject's recommended OnConnectionEvent callback (#907)

## [1.9.0] - 2024-04-18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public AsyncOperation LocalLoadOperation
/// </summary>
public float LocalProgress
{
get => IsSpawned && ProgressTrackers.ContainsKey(NetworkManager.LocalClientId) ?
ProgressTrackers[NetworkManager.LocalClientId].Progress.Value : m_LocalProgress;
get => IsSpawned && ProgressTrackers.ContainsKey(NetworkManager.LocalClientId) ? ProgressTrackers[NetworkManager.LocalClientId].Progress.Value : m_LocalProgress;
private set
{
if (IsSpawned && ProgressTrackers.ContainsKey(NetworkManager.LocalClientId) && ProgressTrackers[NetworkManager.LocalClientId].IsSpawned)
Expand All @@ -69,18 +68,17 @@ public override void OnNetworkSpawn()
{
if (IsServer)
{
NetworkManager.OnClientConnectedCallback += AddTracker;
NetworkManager.OnClientDisconnectCallback += RemoveTracker;
AddTracker(NetworkManager.LocalClientId);
NetworkManager.OnConnectionEvent += OnConnectionEvent;
}
}

public override void OnNetworkDespawn()
{
if (IsServer)
{
NetworkManager.OnClientConnectedCallback -= AddTracker;
NetworkManager.OnClientDisconnectCallback -= RemoveTracker;
NetworkManager.OnConnectionEvent -= OnConnectionEvent;
}

ProgressTrackers.Clear();
onTrackersUpdated?.Invoke();
}
Expand Down Expand Up @@ -120,9 +118,30 @@ void ClientUpdateTrackersRpc()
}
}
}

onTrackersUpdated?.Invoke();
}

void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
if (IsServer)
{
switch (connectionEventData.EventType)
{
case ConnectionEvent.ClientConnected:
{
AddTracker(connectionEventData.ClientId);
break;
}
case ConnectionEvent.ClientDisconnected:
{
RemoveTracker(connectionEventData.ClientId);
break;
}
}
}
}

void AddTracker(ulong clientId)
{
if (IsServer)
Expand All @@ -141,9 +160,7 @@ void RemoveTracker(ulong clientId)
{
if (ProgressTrackers.ContainsKey(clientId))
{
var tracker = ProgressTrackers[clientId];
ProgressTrackers.Remove(clientId);
tracker.NetworkObject.Despawn();
ClientUpdateTrackersRpc();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override void OnNetworkSpawn()
{
// Adding this to remove all pending references to a specific client when they disconnect, since objects
// that are destroyed do not generate OnTriggerExit events.
NetworkManager.OnClientDisconnectCallback += RemovePlayer;
NetworkManager.OnConnectionEvent += OnConnectionEvent;

NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
m_PlayersInTrigger = new List<ulong>();
Expand All @@ -65,7 +65,7 @@ public override void OnNetworkDespawn()
{
if (IsServer)
{
NetworkManager.OnClientDisconnectCallback -= RemovePlayer;
NetworkManager.OnConnectionEvent -= OnConnectionEvent;
NetworkManager.SceneManager.OnSceneEvent -= OnSceneEvent;
}
}
Expand Down Expand Up @@ -136,11 +136,14 @@ void FixedUpdate()
}
}

void RemovePlayer(ulong clientId)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
// remove all references to this clientId. There could be multiple references if a single client owns
// multiple NetworkObjects with the m_PlayerTag, or if this script's GameObject has overlapping colliders
while (m_PlayersInTrigger.Remove(clientId)) { }
if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected)
{
// remove all references to this clientId. There could be multiple references if a single client owns
// multiple NetworkObjects with the m_PlayerTag, or if this script's GameObject has overlapping colliders
while (m_PlayersInTrigger.Remove(connectionEventData.ClientId)) { }
}
}

IEnumerator WaitToUnloadCoroutine()
Expand Down