You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using Fusion;
using UnityEngine;
namespace Example
{
/// <summary>
/// Helper class which iterates over list of active players (NetworkRunner.ActivePlayers) and call spawn/despawn callbacks on demand.
/// It expects 1:1 mapping between a player connection (PlayerRef) and player object (network object with a component of type T).
/// </summary>
public static class PlayerManager<T> where T : SimulationBehaviour
{
private static List<PlayerRef> _tempSpawnPlayers = new List<PlayerRef>();
private static List<T> _tempSpawnedPlayers = new List<T>();
public static void UpdatePlayerConnections(NetworkRunner runner, Action<PlayerRef> spawnPlayer, Action<PlayerRef, T> despawnPlayer)
{
_tempSpawnPlayers.Clear();
_tempSpawnedPlayers.Clear();
// 1. Get all connected players, marking them as pending spawn.
_tempSpawnPlayers.AddRange(runner.ActivePlayers);
// 2. Get all player objects with component of type T.
runner.GetAllBehaviours(_tempSpawnedPlayers);
for (int i = 0; i < _tempSpawnedPlayers.Count; ++i)
{
T player = _tempSpawnedPlayers[i];
PlayerRef playerRef = player.Object.InputAuthority;
// 3. Remove PlayerRef of existing player object from pending spawn list.
_tempSpawnPlayers.Remove(playerRef);
// 4. If a player is not valid (disconnected) execute the despawn callback.
if (runner.IsPlayerValid(playerRef) == false)
{
try
{
despawnPlayer(playerRef, player);
}
catch (Exception exception)
{
Debug.LogException(exception);
}
}
}
// 5. Execute spawn callback for all players pending spawn (recently connected).
for (int i = 0; i < _tempSpawnPlayers.Count; ++i)
{
try
{
spawnPlayer(_tempSpawnPlayers[i]);
}
catch (Exception exception)
{
Debug.LogException(exception);
}
}
// 6. Cleanup
_tempSpawnPlayers.Clear();
_tempSpawnedPlayers.Clear();
}
}
}