Photon Multiplayer Integration for Mobile Games
Photon is the de-facto standard for Unity multiplayer on mobile. Photon Realtime SDK handles transport layer, matchmaking, and room management. But with incorrect integration: connection breaks on network switch, object sync fails, and traffic bill is 3× expected.
PhotonView and Object Sync
PhotonView—core sync component. Each networked object gets ViewID. Position and rotation sync via PhotonTransformView by default—works but broadcasts updates on every FixedUpdate, even if object didn't move.
Correct approach: IPunObservable.OnPhotonSerializeView with manual control:
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
// Send only if significantly changed
if (Vector3.Distance(_lastSentPosition, transform.position) > 0.01f)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
_lastSentPosition = transform.position;
}
}
else
{
_networkPosition = (Vector3)stream.ReceiveNext();
_networkRotation = (Quaternion)stream.ReceiveNext();
}
}
On client, received _networkPosition isn't applied directly—interpolate via Vector3.MoveTowards or Lerp by packet time from PhotonMessageInfo.SentServerTime.
RPC and Reliable Events
photonView.RPC—for events that must reliably arrive: damage, death, item pickup. RpcTarget.All broadcasts to room including sender. RpcTarget.Others—everyone except.
Typical mistake: using RPC for position updates. RPC is reliable (TCP-like), adds acknowledgment overhead. For positions—PhotonNetwork.SendRate + OnPhotonSerializeView. For critical events—RPC.
PhotonNetwork.SendRate default 20, SerializationRate—10. For mobile balance: 15/10.
Connection and Rooms
PhotonNetwork.ConnectUsingSettings(); // uses PhotonServerSettings asset
void OnConnectedToMaster() {
PhotonNetwork.JoinRandomOrCreateRoom(
expectedCustomRoomProperties: null,
expectedMaxPlayers: 4,
matchingType: MatchmakingMode.FillRoom,
typedLobby: null,
sqlLobbyFilter: null,
createIfNotFound: true
);
}
RoomOptions.CustomRoomPropertiesForLobby—array of keys visible in lobby for filtering. Don't pass everything: only fields used for filtering (region, game mode, map).
Mobile Issues
Network switch. Photon supports PhotonNetwork.ReconnectAndRejoin()—but only if room has playerTtl > 0. Default playerTtl = 0, player considered disconnected immediately. For mobile: playerTtl = 10000 (10 seconds for reconnect).
iOS Background. iOS aggressively kills network connections on background. Photon breaks in 5-10 seconds. For critical games—UIBackgroundModes: voip (with caution, Apple may reject) or graceful disconnect.
Traffic. Photon Realtime bills by CCU (concurrent users). 20 CCU free. On integration, add Photon Dashboard to monitoring—see message count, bytes, peaks.
Timeline
Basic Photon Realtime integration with rooms, position sync, and RPC: 3-7 days. Full system with matchmaking, custom properties, and mobile optimization: 2-3 weeks. Cost calculated individually.







