Skip to main content

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 NetworkRunner and 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 PlayerConnectionHost prefab
  • 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 _factory
  • InputIconProvider _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.