【问题标题】:OnJoinedRoom Not Being CalledOnJoinedRoom 未被调用
【发布时间】:2019-10-22 14:03:34
【问题描述】:

当玩家加入房间时,我想根据哈希表为他们分配某个角色。
然而似乎OnJoinedRoom 甚至没有被调用,因为Debug.Log 调用没有出现。

我检查了我使用的是MonoBehaviourPunCallbacks。我正在使用 Unity(2019.1.0f2 个人版)和 PUN 2

public class GameHandler : MonoBehaviourPunCallbacks
{
    public readonly string[] ROLES = {
        "Villager",
        "Werewolf",
        "Seer",
        "Werewolf",
        "Doctor",
        "Villager"
    };

    Hashtable PlayerProperties = new Hashtable
    {
        {"Role", null}, 
        {"Team", null}
    };

    public TMP_Text ROLE_TXT;

    private void Start()
    {
        PhotonNetwork.LocalPlayer.SetCustomProperties(PlayerProperties);
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("OnJoinedRoomCalled");
        GetRoleAndTeam();
    }

    private void GetRoleAndTeam()
    {
        PlayerProperties["Role"] = ROLES[PhotonNetwork.CurrentRoom.PlayerCount - 1];

        PhotonNetwork.LocalPlayer.SetCustomProperties(PlayerProperties);        

        string MyRole = (string) PhotonNetwork.LocalPlayer.CustomProperties["Role"];

        Debug.Log("My Role :");

        ROLE_TXT.text = MyRole;

        if (MyRole.Equals("Werewolf"))
        {
            PlayerProperties["Team"] = "Werewolves";

            PhotonNetwork.LocalPlayer.SetCustomProperties(PlayerProperties); 
        }

        else
        {
            PlayerProperties["Team"] = "Villagers";

            PhotonNetwork.LocalPlayer.SetCustomProperties(PlayerProperties);
        }
    }
}

我希望它调用OnJoinedRoom,因此它运行Debug.Log(…) 和GetRoleAndTeam(),但OnJoinedRoom 没有被调用。

【问题讨论】:

  • 你确定他们真的加入了房间吗?你怎么知道的?
  • 哦,这可能是个问题,当玩家创建房间时,他们会加入吗? PhotonNetwork.CurrentRoom 中的所有功能都可以正常工作,当只有我在房间里时,PhotonNetwork.CurrentRoom.PlayerCount 返回 1。
  • 是否调用了其他 PUN 回调,例如 OnConnectedToMaster
  • 其他回调如 OnConnectedToMaster、OnCreateRoom 和 OnJoinedLobby 都可以正常工作
  • 创建房间的玩家加入房间,因此应该调用两个回调(OnCreateRoomOnJoinedRoom)。代码丢失或您从另一个脚本加入房间?你怎么加入房间?向Start 添加日志以查看GameHandler 是否已启用并处于活动状态。

标签: c# unity3d photon


【解决方案1】:

设置

PhotonNetwork.AutomaticallySyncScene = false;

当你使用 Automatically Sync Scene 时,连接到已创建房间的玩家会立即开始加载 Master 拥有的场景并跳过 OnJoinedRoom 回调。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2016-05-21
    • 2012-02-15
    • 2011-11-17
    • 2016-04-03
    相关资源
    最近更新 更多