为什么PUN没有给出回调? ProtonNetworking没有按预期工作

时间:2017-03-18 11:22:01

标签: c# unity3d

我正在尝试使用PUN进行简单的步骤,只需连接到主服务器,然后加入随机房间。 我已将日志所有信息设置为控制台,据我所知,我已连接到服务器,但从未调用过OnConnectedToMaster。

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     using Photon;
 public class  RandomMatchmakera : Photon.PunBehaviour {

/// <summary>
/// MonoBehaviour method called on GameObject by Unity during early initialization phase.
/// </summary>
void Avake(){
    // this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
    PhotonNetwork.automaticallySyncScene = true;


}

void Start(){
    // the following line checks if this client was just created (and not yet online). if so, we connect
    if (PhotonNetwork.connectionStateDetailed == ClientState.PeerCreated)
    {
        Debug.Log ("connecting");
        // Connect to the photon master-server. We use the settings saved in PhotonServerSettings (a .asset file in this project)
        PhotonNetwork.ConnectUsingSettings("0.9");
    }
}

public void OnJoinedLobby()
{
    Debug.Log("DemoAnimator/Launcher: OnJoinedLobby() was called by PUN");
    PhotonNetwork.JoinRandomRoom();
}
public override void OnConnectedToMaster()
{
    Debug.Log("DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN");
    PhotonNetwork.JoinRandomRoom();
}

public override void OnDisconnectedFromPhoton()
{

    Debug.LogWarning("DemoAnimator/Launcher: OnDisconnectedFromPhoton() was called by PUN");        
}

public override void OnPhotonRandomJoinFailed(object[] codeAndMsg){
    Debug.LogWarning("DemoAnimator/Launcher: Failing joining random room");
    PhotonNetwork.CreateRoom (null, new RoomOptions (){ MaxPlayers = 4 }, null);
}
public override void OnJoinedRoom(){
    Debug.LogWarning("DemoAnimator/Launcher: Joined room");
}


public void OnFailedToConnectToPhoton(object parameters)
{
    Debug.Log("OnFailedToConnectToPhoton. StatusCode: " + parameters + " ServerAddress: " + PhotonNetwork.ServerAddress);
}
}

以下是loggs。

Firstlog

Secondlog

2 个答案:

答案 0 :(得分:0)

我刚刚复制粘贴你的解决方案并成功调用它。假设你正在使用PUN资产和Marco Polo示例中的PhotonTutorials,我认为你没有从你的项目中引用RandomMatchmakera脚本。Here is how you do it

答案 1 :(得分:0)

我希望这会对某人有所帮助。对我来说问题出在我的地区。我选择了我的区域为none,而不是将其指定到特定区域。因此找到

  

PhotonServerSettings

归档并将区域更改为特定的内容,例如EU,Kr等