在Unity中使用Photon,在离开房间后,用户无法通过房间名称连接到房间。

huangapple go评论71阅读模式
英文:

Using Photon in Unity after leaving room user can't connect to room by name

问题

以下是您提供的代码的中文翻译:

用户连接到大厅:

private void Start()
{
    PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster()
{
    PhotonNetwork.JoinLobby();
    SceneManager.LoadScene("Lobby");
}

然后用户通过房间名创建房间或进入房间:

private Dictionary<string, RoomInfo> _roomList = new Dictionary<string, RoomInfo>();

private bool CheckNullOrEmpty(string name)
{
    return !string.IsNullOrEmpty(name);
}

private bool CheckServerAvaliable(string qwe)
{
    bool isAvaliable = true;

    foreach (var item in _roomList)
    {
        RoomInfo info = item.Value;

        if (info.Name == qwe)
        {
            isAvaliable = false;
        }
    }

    return isAvaliable;
}

private void SetNickname()
{
    PhotonNetwork.NickName = _nickname.text;
    PlayerPrefs.SetString("name", _nickname.text);
}

private void CreateRoom()
{
    RoomOptions roomOptions = new() { MaxPlayers = _maximumPlayers };
    PhotonNetwork.CreateRoom(_createServer.text, roomOptions);
}

public void TryCreateRoom()
{
    if (CheckNullOrEmpty(_createServer.text) && CheckServerAvaliable(_createServer.text) && CheckNullOrEmpty(_nickname.text))
    {
        SetNickname();

        CreateRoom();
    }

    else if (!CheckNullOrEmpty(_nickname.text))
    {
        _errorText.SetText("昵称不能只包含空格或为空");
    }

    else if (!CheckServerAvaliable(_createServer.text))
    {
        _errorText.SetText("房间名称已被占用");
    }
}

public void TryJoinRoom()
{
    if (!CheckServerAvaliable(_joinServer.text))
    {
        SetNickname();

        PhotonNetwork.JoinRoom(_joinServer.text);
    }

    else if (!CheckNullOrEmpty(_nickname.text))
    {
        _errorText.SetText("昵称不能只包含空格或为空");
    }

    else if (CheckServerAvaliable(_joinServer.text))
    {
        _errorText.SetText("没有该名称的房间");
    }
}

private void UpdateCachedRoomList(List<RoomInfo> roomList)
{
    for (int i = 0; i < roomList.Count; i++)
    {
        RoomInfo info = roomList[i];
        if (info.RemovedFromList)
        {
            _roomList.Remove(info.Name);
        }
        else
        {
            _roomList[info.Name] = info;
        }
    }
}

public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
    UpdateCachedRoomList(roomList);
}

一切都正常工作,然后用户离开房间并返回大厅:

public void Leave()
{
    PhotonNetwork.LeaveRoom();
    SceneManager.LoadScene("Lobby");
}

在大厅中创建房间运行正常,但进入房间不起作用,我收到 "没有该名称的房间" 的消息。我已注释掉服务器存在的验证部分,然后用户连接,但我不理解验证中的问题。

英文:

Users connect to lobby:

private void Start()
{
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
SceneManager.LoadScene(&quot;Lobby&quot;);
}

Then users create room or enter in a room by its name

private Dictionary&lt;string, RoomInfo&gt; _roomList = new Dictionary&lt;string, RoomInfo&gt;();
private bool CheckNullOrEmpty(string name)
{
return !string.IsNullOrEmpty(name);
}
private bool CheckServerAvaliable(string qwe)
{
bool isAvaliable = true;
foreach (var item in _roomList)
{
RoomInfo info = item.Value;
if (info.Name == qwe)
{
isAvaliable = false;
}
}
return isAvaliable;
}
private void SetNickname()
{
PhotonNetwork.NickName = _nickname.text;
PlayerPrefs.SetString(&quot;name&quot;, _nickname.text);
}
private void CreateRoom()
{
RoomOptions roomOptions = new() { MaxPlayers = _maximumPlayers };
PhotonNetwork.CreateRoom(_createServer.text, roomOptions);
}
public void TryCreateRoom()
{
if (CheckNullOrEmpty(_createServer.text) &amp;&amp; CheckServerAvaliable(_createServer.text) &amp;&amp; CheckNullOrEmpty(_nickname.text))
{
SetNickname();
CreateRoom();
}
else if (!CheckNullOrEmpty(_nickname.text))
{
_errorText.SetText(&quot;Nickname cannot contain only spaces or be empty&quot;);
}
else if (!CheckServerAvaliable(_createServer.text))
{
_errorText.SetText(&quot;The name of the room is occupied&quot;);
}
}
public void TryJoinRoom()
{
if (!CheckServerAvaliable(_joinServer.text))
{
SetNickname();
PhotonNetwork.JoinRoom(_joinServer.text);
}
else if (!CheckNullOrEmpty(_nickname.text))
{
_errorText.SetText(&quot;Nickname cannot contain only spaces or be empty&quot;);
}
else if (CheckServerAvaliable(_joinServer.text))
{
_errorText.SetText(&quot;There is no room with such name&quot;);
}
}
private void UpdateCachedRoomList(List&lt;RoomInfo&gt; roomList)
{
for (int i = 0; i &lt; roomList.Count; i++)
{
RoomInfo info = roomList[i];
if (info.RemovedFromList)
{
_roomList.Remove(info.Name);
}
else
{
_roomList[info.Name] = info;
}
}
}
public override void OnRoomListUpdate(List&lt;RoomInfo&gt; roomList)
{
UpdateCachedRoomList(roomList);
}

Everything works well, then users leave room and enter lobby:

  public void Leave()
{
PhotonNetwork.LeaveRoom();
SceneManager.LoadScene(&quot;Lobby&quot;);
}

In lobby creating room works fine, but entering to room doesn't work, i get - There is no room with such name.
I commented verification of server existing, and then user connected, i don't understand what is the problem in verification

答案1

得分: 1

通过简单地添加以下代码解决了这个问题:

public override void OnConnectedToMaster()
{
    if (!PhotonNetwork.InLobby)
        PhotonNetwork.JoinLobby();
}
英文:

Solved the problem by simple adding

public override void OnConnectedToMaster()
{
if (!PhotonNetwork.InLobby)
PhotonNetwork.JoinLobby();
}

答案2

得分: 0

问题出在条件的顺序上。目前,您首先检查服务器是否可用(!CheckServerAvaliable(_joinServer.text)),然后检查昵称是否为空或仅包含空格(!CheckNullOrEmpty(_nickname.text)),最后再次检查服务器是否可用(CheckServerAvaliable(_joinServer.text))。

由于您收到了"没有这个名称的房间"的错误消息,这表明第二个条件(else if (!CheckNullOrEmpty(_nickname.text)))被评估为真,并显示了错误消息。

public void TryJoinRoom()
{
    if (!CheckNullOrEmpty(_nickname.text))
    {
        _errorText.SetText("昵称不能仅包含空格或为空");
    }
    else if (!CheckServerAvaliable(_joinServer.text))
    {
        SetNickname();
        PhotonNetwork.JoinRoom(_joinServer.text);
    }
    else if (CheckServerAvaliable(_joinServer.text))
    {
        _errorText.SetText("没有这个名称的房间");
    }
}
我认为这段代码将解决您的问题。
英文:
  • The issue lies in the order of your conditionals. Currently, you are checking if the server is available (!CheckServerAvaliable(_joinServer.text)) first, and then checking
  • if the nickname is empty or contains only spaces (!CheckNullOrEmpty(_nickname.text)), and finally checking if the server is available again (CheckServerAvaliable(_joinServer.text)).

Since you are getting the "There is no room with such name" error, it indicates that the second conditional (else if (!CheckNullOrEmpty(_nickname.text))) is evaluated to true, and the error message is displayed.

 public void TryJoinRoom()
{
if (!CheckNullOrEmpty(_nickname.text))
{
_errorText.SetText(&quot;Nickname cannot contain only spaces or be empty&quot;);
}
else if (!CheckServerAvaliable(_joinServer.text))
{
SetNickname();
PhotonNetwork.JoinRoom(_joinServer.text);
}
else if (CheckServerAvaliable(_joinServer.text))
{
_errorText.SetText(&quot;There is no room with such name&quot;);
}
}

i think this code will solve your problem

huangapple
  • 本文由 发表于 2023年7月10日 19:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定