Photon Unity Networking基础教程 4 Ga

作者: 浪尖儿 | 来源:发表于2016-10-28 13:41 被阅读553次

这部分介绍了如何添加处理不同级别加载的功能,该功能基于目前房间中的玩家数量。

主要内容

  • 加载Arena Routine
  • 观察玩家连接
  • 从大厅加载Arena

加载Arena Routine

我们创造了4个不同的房间,并且按照约定命名他们,最后一个字符是玩家的数量,所以现在很容易绑定当前房间和关联场景球员数量。这是一种被称为“convention over configuration”的非常有效的技术,基于“Configuration”的方法,举例来说,关于房间中给定数量的玩家,已经维护了一个场景名字查找表。我们的脚本然后会查看该列表,并返回一个无关的场景。 “Configuration”一般需要更多的代码,这就是为什么我们将在这里使用“Convention”,让我们更容易实现想要的功能,而不会让无关的功能污染我们的代码。

  1. 打开GameManager脚本

  2. 在private methods区块内添加一个新方法,不要忘记保存

     #region Private Methods
     void LoadArena()
     {
         if (!PhotonNetwork.isMasterClient)
         {
             Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
         }
         Debug.Log("PhotonNetwork : Loading Level : " + PhotonNetwork.room.playerCount);
         PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.room.playerCount);
     }
     #endregion
    
  3. 保存GameManager脚本

当我们调用这个方法时,我们将根据我们所在的房间的playerCount属性加载适当的房间。

在这里有两件事要注意,这是非常重要的。

  • 如果我们是master,PhotonNetwork.LoadLevel()才应该被调用。所以我们使用PhotonNetwork.isMasterClient首先检查是否是master。检查这一点是调用者的责任,我们将在本节的下一部分中介绍。
  • 我们使用PhotonNetwork.LoadLevel()加载我们想要的级别,我们不直接使用Unity,因为我们想依靠Photon加载这个级别,使在房间中所有连接的客户端都生效,因为我们在这个游戏中启用了PhotonNetwork.automaticallySyncScene。

现在我们可以加载正确的级别,让我们绑定到玩家的连接和断开上面。

观察玩家连接

目前,我们的GameManager脚本是一个常规的MonoBehaviour,我们在前面的教程中学习了使用Photon回调的各种方法,现在GameManager需要监听玩家的连接和断开连接。让我们实现这个。

  1. 打开GameManager脚本

  2. 把当前的基类MonoBehaviour修改为Photon.PunBehaviour

    public class GameManager : Photon.PunBehaviour {

  3. 添加下面的Photon回调信息

     public override void OnPhotonPlayerConnected(PhotonPlayer other)
     {
         Debug.Log("OnPhotonPlayerConnected() " + other.name); // not seen if you're the player connecting
         if (PhotonNetwork.isMasterClient)
         {
             Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
             LoadArena();
         }
     }
    
     public override void OnPhotonPlayerDisconnected(PhotonPlayer other)
     {
         Debug.Log("OnPhotonPlayerDisconnected() " + other.name); // seen when other disconnects
         if (PhotonNetwork.isMasterClient)
         {
             Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
             LoadArena();
         }
     }
    
  4. 保存GameManager脚本

现在,我们有了一个完整的设置。每当玩家加入或离开房间时,我们都会被通知,我们将调用我们刚才创建的LoadArena()方法。但是,只有当我们是PhotonNetwork.isMasterClient的情况下,我们才调用LoadArena()。

让我们现在回到Lobby,最终加入房间的时候能够加载正确的场景。

从大厅加载Arena

  1. 编辑脚本Launcher

  2. 把下面的脚本添加到OnJoinedRoom方法

     // #Critical: We only load if we are the first player, else we rely on  PhotonNetwork.automaticallySyncScene to sync our instance scene.
     if (PhotonNetwork.room.playerCount == 1)
     {
         Debug.Log("We load the 'Room for 1' ");
         // #Critical
         // Load the Room Level. 
         PhotonNetwork.LoadLevel("Room for 1");
     }
    
  3. 保存脚本

让我们测试一下,打开Launcher场景,运行它。点击“play”,让系统连接并加入一个房间。就这样,Lobby没有问题。但如果你离开房间,你会注意到,当回到大厅时,它会自动重新加入房间。噢,让我们解决一下这个问题。

如果你还不知道为什么,简单地分析一下日志。我只是简单地说说一下,因为这需要实践和经验来解决这个问题,知道问题出在哪里以及如何调试它。

你自己尝试一下,如果你仍然找不到问题的根源,让我们一起做。

  1. 运行Launcher场景
  2. 点击Play按钮,等到你加入了一个房间,"Room for 1"加载完毕
  3. 清空Unity Console
  4. 点击“Leave Room”
  5. 研究一下Unity Console,注意记录中有这句话 "DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN"
  6. 停止Launcher场景
  7. 再Unity Console中双击这句话 "DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN",会打开脚本并指向调用的那一行
  8. 嗯...每次我们连接上的时候,都会自动的加入一个房间JoinRandomRoom,但是这并不是我们想要的。

要解决这个问题,我们需要知道上下文。当用户点击“Play”按钮时,我们应该记下一个标志,以便知道连接过程源于用户。然后我们可以在各种Photon回调中,检查这个标志,执行相应代码。

  1. 编辑Launcher脚本

  2. 在Private Variables部分创建一个新的属性

     /// <summary>
     /// Keep track of the current process. Since connection is asynchronous and is based on several callbacks from Photon, 
     /// we need to keep track of this to properly adjust the behavior when we receive call back by Photon.
     /// Typically this is used for the OnConnectedToMaster() callback.
     /// </summary>
     bool isConnecting;
    
  3. 在connect()方法开头加入下面代码

     // keep track of the will to join a room, because when we come back from the game we will get a callback that we are connected, so we need to know what to do then
     isConnecting = true;
    
  4. 在OnConnectedMaster()方法中,在PhotonNetwork.JoinRandomRoom()外面加一个if语句

     // we don't want to do anything if we are not attempting to join a room. 
     // this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
     // we don't want to do anything.
     if (isConnecting)
     {
         // #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnPhotonRandomJoinFailed()
         PhotonNetwork.JoinRandomRoom();
     }
    
  5. 保存脚本

现在,如果我们再次测试并运行启动场景,并在大厅和游戏之间来回切换,一切都很好:)为了测试场景的自动同步,您需要发布应用程序(发布桌面,它运行测试最快),并在Unity之外运行它,所以你有了两个玩家,将连接和加入一个房间。 如果Unity Editor首先创建房间,它将是MasterClient,您将能够在Unity控制台中验证您在连接时获得“PhotonNetwork:Loading Level:1”和更高版本“PhotonNetwork:Loading Level:2” 发布的实例。

好!我们已经讲了很多,但这只是工作的一半... :)我们将在下一节需要解决玩家本身问题。不要忘记休息,以更有效地吸收所解释的各种概念。

如果您对某个功能有疑问,或者您在阅读本教程后遇到问题,并遇到错误或未在此处提及的问题,欢迎在论坛上提出问题,我们将竭诚为您服务:)

原文

http://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/gamemanager-levels

相关文章

网友评论

  • 魁犸:这个系列的文章不错!但是如果能多一些配图就更好了
    浪尖儿: @魁犸 好啊,有时间再多加一些图😀

本文标题:Photon Unity Networking基础教程 4 Ga

本文链接:https://www.haomeiwen.com/subject/nvceuttx.html