统一与光子引擎。场景重载后 Joining to Lobby, CreateRoom 和 Join Random 函数重复调用

Unity & PhotonEngine. After scene reaload Joining to Lobby, CreateRoom and JoinRandom functions called repeatedly

我运行在使用光子网络的测试项目的最后一步遇到了问题。当您第一次连接并加入房间时,一切都没有错误。但是,在完成匹配,退出房间,使用LoadScene()后,出现错误:

JoinLobby operation (229) not called because client is not connected or not yet ready, client state: JoiningLob <- 在 OnConnectedToMaster()

通过经验,我意识到ConnectUsingSettings()方法和其他Photon方法被多次调用。但是连接到大厅,我可以创建一个房间,但我立即遇到 MissingReferenceException 错误。

我看到有人 运行 解决了同样的问题。问题的出现是因为 事件 。无论在哪里发生这种情况,我都会退订这些活动,但这无济于事。还有什么会导致这样的问题,因为我显然错过了一些阻止我在 t运行sition?

期间完全关闭场景的东西

对不起我的语言,使用 Google T运行slate

代码: LobbyManager.cs

private void StartConnect()
{
     PhotonNetwork.NickName = master.GameSettings.NickName;
     PhotonNetwork.GameVersion = master.GameSettings.NickName;
     PhotonNetwork.ConnectUsingSettings();
     PhotonNetwork.AutomaticallySyncScene = true;
}
public override void OnConnectedToMaster()
{
     Debug.Log("Connected to server");
     if(!PhotonNetwork.InLobby) PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
     onConnected.Invoke();//This use for show UIElements on Canvas
}

加入随机房间class

public void OnClick_JoinRandomRoom()
{
     if (!PhotonNetwork.IsConnected) return;
              
     if (GameModeGlobalData.SelectedGameMode != null)
     {
           SetRoomOptions();
           PhotonNetwork.JoinRandomRoom(expectedRoomProperties, GameModeGlobalData.SelectedGameMode.MaxPlayers);
     }
}
    
public override void OnJoinRandomFailed(short returnCode, string message)
{
     Debug.Log("Join random failed: " + message + ". Room will be created...");
            _createRoomMenu.CreateAndJoinRoom();
}
    
public void SetRoomOptions()
{
     expectedRoomProperties[RoomData.GAME_MODE] = GameModeGlobalData.SelectedGameMode.GameModeName;
}

 
private void OnDisable()
{
      ShowPanels.RemoveAllListeners();
} 

CreateRoom.cs

private ExitGames.Client.Photon.Hashtable _roomCustomProperties = new ExitGames.Client.Photon.Hashtable();
public void CreateAndJoinRoom()
    {
        if (!PhotonNetwork.IsConnected) return;
        if (GameModeGlobalData.SelectedGameMode != null)
        {
            RoomOptions roomOptions = GetCustomRoomOptions();
            roomOptions.CleanupCacheOnLeave = true;
            PhotonNetwork.CreateRoom(randomRoomName, roomOptions);
        }
    }
    public RoomOptions GetCustomRoomOptions()
    {
        RoomOptions options = new RoomOptions();
        options.MaxPlayers = _maxPlayer;
        options.IsOpen = true;
        options.IsVisible = true;

        string[] roomProperties = new string[]{ RoomData.GAME_MODE };
        _roomCustomProperties[RoomData.GAME_MODE] = GameModeGlobalData.SelectedGameMode.GameModeName;

        options.CustomRoomPropertiesForLobby = roomProperties;
        options.CustomRoomProperties = _roomCustomProperties;
        return options;
    }

项目变大了,怪自己一开始没有测试。没想到这个阶段会有问题

对此感到抱歉 post。它解决了。对于那些将来可能会遇到这种情况的人,除了取消订阅事件之外,还要检查从 MonoBehaviourPunCallbacks 继承的所有 类 是否有被覆盖的 OnDisable() 方法。 像这样:

public override void OnDisable()
{
     base.OnDisable();
}

这又会调用

PhotonNetwork.RemoveCallbackTarget(this);

此外,来自文档:

Do not add new MonoBehaviour.OnEnable or MonoBehaviour.OnDisable. Instead, you should override those and call base.OnEnable and base.OnDisable.

忘记了,用了MonoBehaviour.OnDisable