MultiplayerService
The MultiplayerService class provides the central runtime logic for managing multiplayer game sessions using Photon Fusion in Unity. It handles player connections, scene loading, control scheme integration, and reward or item transactions through Firebase-backed orchestrators.
๐งฉ Overviewโ
public class MultiplayerService : SimulationBehaviour, INetworkRunnerCallbacks, IDependencyProvider, IInjectorReceivable
This class is injectable and globally accessible via GameServiceFactory, and orchestrates network runners, player connection management, and Fusion lifecycle hooks.
๐งช Key Componentsโ
Propertiesโ
GameServiceFactory Factoryโ Injected orchestrator service hub.InputProvider ActiveInputProviderโ Current input routing object.ServerScene ActiveSceneโ Scene currently bound to the multiplayer session.Dictionary<PlayerRef, PlayerConnectionHost> playerConnectionsโ Player mapping.
๐ฎ Multiplayer Session Flowโ
StartLocalPlayer(...)โ
Starts a local multiplayer session.
public async Task<bool> StartLocalPlayer(ServerScene scene, string sessionName, ControlScheme controls)
- Spawns
NetworkRunnerand loads addressable scenes - Injects input providers and state callbacks
- Joins or creates session using
Fusion.Runner.StartGame
EndCurrentSession()โ
Cleans up all multiplayer session state.
- Shuts down runner
- Unloads scenes
- Releases assets
- Clears input and connection references
๐น Input Scheme Handlingโ
ChangeControlScheme(...)โ
Switches the active input mapping at runtime:
public bool ChangeControlScheme(ControlScheme newScheme)
Also triggers icon updates via InputIconProvider.
Input Propagationโ
All input actions are piped through:
Action<InputAction, bool> InputCallback
To allow binding external systems (e.g., UI or gameplay modules) to global input.
๐ฅ Player Managementโ
On Player Join/Leaveโ
When a player joins:
public async void OnPlayerJoined(...)
- Instantiates a
PlayerConnectionHostprefab - Tracks via
playerConnections
On player leave:
public async void OnPlayerLeft(...)
- Despawns player host
- Removes from tracking
Manual registration via:
public void RegisterPlayerConnection(PlayerConnectionHost host)
public void UnregisterPlayerConnection(PlayerConnectionHost host)
๐ Transactions and Rewardsโ
PurchaseItem(...)โ
Purchases an in-game item through orchestrator:
public async Task<bool> PurchaseItem(TransactionalItemData_Base_SO item)
Currently supports BuildItemData.
GrantRewardsToPlayer(...)โ
Grants multiple item rewards via orchestrator service:
public async Task<bool> GrantRewardsToPlayer(List<Reward> rewards)
๐ Logout & Cleanupโ
Logout()โ
Cleans up network session and logs the user out of Firebase:
public async void Logout()
๐ Dependency Injectionโ
Implements IInjectorReceivable and IDependencyProvider. Injected members:
GameServiceFactory _factoryInputIconProvider _iconProvider
Provides:
[Provide]
public MultiplayerService GetMultiplayerService()
๐ Fusion Callbacks Implementedโ
This class implements all INetworkRunnerCallbacks, mostly as no-ops unless specifically overridden (e.g., OnPlayerJoined).
๐ง Summaryโ
MultiplayerService is a comprehensive multiplayer session controller for m00mโs Unity game. It seamlessly integrates network setup, input schema, dependency injection, and Firebase-based item transactions into a single, orchestrated runtime layer.