Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?

More than 1 year has passed since last update.

@azarashin

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

環境

  • 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 が見えるようになった。

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What is going on with this article?