【发布时间】: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 都可以正常工作
-
创建房间的玩家加入房间,因此应该调用两个回调(
OnCreateRoom和OnJoinedRoom)。代码丢失或您从另一个脚本加入房间?你怎么加入房间?向Start添加日志以查看GameHandler是否已启用并处于活动状态。