简体   繁体   English

Photon/Unity - 尝试退出游戏场景进入菜单场景时遇到错误

[英]Photon/Unity - Attempting to exit the game scene into the menu scene hits an error

Upon entering the game, I instantiate the menu and connect to the photon server.进入游戏后,我实例化菜单并连接到光子服务器。 In game scene i set onClick event for button to call the method LeaveRoom() .在游戏场景中,我为按钮设置了 onClick 事件以调用方法LeaveRoom()

public class ControlUI : MonoBehaviourPunCallbacks
{
    public void LeaveRoom()
    {
        Destroy(RoomManager.Instance.gameObject);
        StartCoroutine(Disconnect());
    }

    private IEnumerator Disconnect()
    {
        PhotonNetwork.LeaveRoom();
        while (PhotonNetwork.InRoom)
            yield return null;
        SceneManager.LoadScene("Lobby");
    }
}

In result i leave game scene and exit to menu, but in console i get:结果我离开游戏场景并退出菜单,但在控制台中我得到:

Operation SetProperties (252) not called because client is not connected or not ready yet, client state: Leaving
UnityEngine.Debug:LogError (object)
Photon.Realtime.LoadBalancingClient:DebugReturn (ExitGames.Client.Photon.DebugLevel,string) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2564)
Photon.Realtime.LoadBalancingClient:CheckIfOpCanBeSent (byte,Photon.Realtime.ServerConnection,string) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2493)
Photon.Realtime.LoadBalancingClient:OpSetPropertiesOfRoom (ExitGames.Client.Photon.Hashtable,ExitGames.Client.Photon.Hashtable,Photon.Realtime.WebFlags) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2141)
Photon.Realtime.Room:SetCustomProperties (ExitGames.Client.Photon.Hashtable,ExitGames.Client.Photon.Hashtable,Photon.Realtime.WebFlags) (at Assets/Photon/PhotonRealtime/Code/Room.cs:430)
Photon.Pun.PhotonNetwork:SetLevelInPropsIfSynced (object) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2178)
Photon.Pun.PhotonNetwork:NewSceneLoaded () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1475)
Photon.Pun.PhotonHandler/<>c:<Start>b__13_0 (UnityEngine.SceneManagement.Scene,UnityEngine.SceneManagement.LoadSceneMode) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:126)
UnityEngine.SceneManagement.SceneManager:Internal_SceneLoaded (UnityEngine.SceneManagement.Scene,UnityEngine.SceneManagement.LoadSceneMode)

I found this article and tried to make a call in a similar way:我找到了这篇文章并尝试以类似的方式拨打电话:

public void LeaveRoom()
{
    PhotonNetwork.LeaveRoom();
}
 
public override void OnLeftRoom()
{
    SceneManager.LoadScene("Lobby");
 
    base.OnLeftRoom();
}

but it didn't work for me.但这对我不起作用。 By the button phrase my gameController (player) is removed from the room, but the scene doesn't load, the scene doesn't trigger the call OnLeftRoom() .通过按钮短语,我的游戏控制器(玩家)从房间中移除,但场景没有加载,场景不会触发OnLeftRoom()调用。

Don't destroy RoomManager before you leave.离开前不要破坏 RoomManager。 Try something like this尝试这样的事情

public void BackToLobby()
{
    StartCoroutine(IEBackToLobby());
}

IEnumerator IEBackToLobby()
{
    if (PhotonNetwork.InRoom)
    {
        PhotonNetwork.LeaveRoom();
        Destroy(RoomManager.Instance.gameObject);
    }
    else
        yield return null;

    SceneManager.LoadScene("Lobby");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM