PUN Essential API Reference

Current Room Player Count

int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;

Checking Ownership of a PhotonView

When using scripts that inherit from MonoBehaviourPun, you can determine if the current instance belongs to the local player:

bool isOwner = photonView.IsMine;

Checking if a Player is Local

bool isLocal = somePlayer.IsLocal;

Checking Connecsion Status

bool isConnected = PhotonNetwork.IsConnected;

This returns false in offline mode. The offline mode setting is configured in PhotonServerSettings. When Start In Offline Mode is enabled, the project runs without connecting to a server.

Since photonView.IsMine only works while connected, it will throw errors during offline testing. Use this check to handle offline scenarios gracefully:

if (PhotonNetwork.IsConnected && !photonView.IsMine)
{
    return;
}

// Continue with offline logic

Current Network Connection State

string connectionState = PhotonNetwork.NetworkClientState.ToString();

Setting Player Nickname

PhotonNetwork.NickName = "PlayerName";

Creating Rooms

// Create a room with maximum player capacity
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = maxPlayersPerRoom });

// Create a room with a specific name and player limit
PhotonNetwork.CreateRoom("RoomName", new RoomOptions { MaxPlayers = maxPlayers }, null);

Syncing Custom Properties

Sending Custom Properties

using ExitGames.Client.Photon;

var properties = new Hashtable { { "IsPlayerReady", true } };
PhotonNetwork.LocalPlayer.SetCustomProperties(properties);

Receiving Custom Properties

foreach (Player player in PhotonNetwork.PlayerList)
{
    Debug.Log(player.NickName);

    object readyStatus;
    if (player.CustomProperties.TryGetValue("IsPlayerReady", out readyStatus))
    {
        bool isReady = (bool)readyStatus;
        Debug.Log(isReady ? "Player is ready" : "Player is not ready");
    }
}

Retrieving All Custom Properties

Debug.Log(targetPlayer.CustomProperties.ToStringFull());

Player Score

PUN provides built-in score synchronization.

Setting Score

PhotonNetwork.LocalPlayer.SetScore(0);

Getting Score

foreach (Player player in PhotonNetwork.PlayerList)
{
    Debug.Log($"Player: {player.NickName}, Score: {player.GetScore()}");
}

Getting All Players in Room

foreach (Player player in PhotonNetwork.PlayerList)
{
    Debug.Log(player.NickName);
}

Player ID (ActorNumber)

The ActorNumber serves as a unique identifier for players within a room. It changes when a player rejoins and is unique per room. Outside a room, it returns -1.

foreach (Player player in PhotonNetwork.PlayerList)
{
    Debug.Log($"Player ID: {player.ActorNumber}");
}

Player Number

Note: The PlayerNumbering script must be attached to an object in the scene for this property to work.

Unlike ActorNumber, the player number is sequential starting from 0 based on join order. A value of -1 indicates the property is unavailable (script not attached).

foreach (Player player in PhotonNetwork.PlayerList)
{
    Debug.Log($"Player Number: {player.GetPlayerNumber()}");
}

PlayerNumbering Callback Delegate

When players join or leave a room, the numbering system updates accordingly. The following callback fires whenever the numbering changes:

PlayerNumbering.OnPlayerNumberingChanged += OnPlayerNumberingChanged;

void OnPlayerNumberingChanged()
{
    // Update UI elements based on new player numbers
}

This is useful for updating player indicators or color assignments dynamically when the player list changes.

Tags: PUN Photon unity networking Multiplayer

Posted on Sat, 09 May 2026 15:35:46 +0000 by allydm