2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PUN2 でUserId がnull になっている時の対処法

Posted at

環境

  • Windows 10
  • Unity 2019.2.19f1
  • Photon2 (Photon Unity Networking 2 2.16)

症状

public class NetworkInterface : MonoBehaviourPunCallbacks
{

    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
        PhotonNetwork.JoinOrCreateRoom(_config.RoomName, new RoomOptions(), TypedLobby.Default);
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("Room Max number: " + PhotonNetwork.CurrentRoom.MaxPlayers); 
        Player[] player = PhotonNetwork.PlayerList;

        // プレイヤー名とIDを表示.
        for (int i = 0; i < player.Length; i++)
        {
            Debug.Log((i).ToString() + " : " + player[i].NickName + " ID = " + player[i].UserId);
        }
    }
}

上記のコードでルームに数人が入る。最後に入室した人の場合、PhotonNetwork.PlayerList に全員登録されているはずなので、NickName とUserId がすべて入っているはずだが、UserId が自分のもの以外null になっている。

対策

RoomOptions を生成するときの、デフォルト値のままではなく、

public class NetworkInterface : MonoBehaviourPunCallbacks
{

    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
        RoomOptions options = new RoomOptions();
        options.PublishUserId = true; // ★お互いにユーザIDが見えるようにする。
        options.MaxPlayers = 3; // ★最大人数もきちんと定義しておく。
        PhotonNetwork.JoinOrCreateRoom(_config.RoomName, options, TypedLobby.Default);
     }

    public override void OnJoinedRoom()
    {
...
    }
}

RoomOptions のPublishUserId はデフォルト値がfalse のようなので、true に変更してJoinOrCreateRoom すると意図通りUserId が見えるようになった。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?