王者荣耀实时对战服务器Photon之PUN场景管理

发表于2017-11-18
评论0 2.3k浏览

本节将介绍如何根据房间中玩家的人数来处理加载各种级别的房间,让玩家顺利进入使用Photon创建对应房间的功能。之前不同的房间场景的创建在上一节教程完成,PUN基本入门之游戏场景构建

我们创建了4个不同的房间,我们以玩家人数来进行命名它们,所以现在很容易区分当前玩家数量和相关场景。 这是一种非常有效的方法。 我们的脚本会在房间列表中查找,并返回一个场景的名称。 “配置”通常需要多个的脚本来实现,那我们在这里进行“约定”,这样我们可以更快地实现工作代码,而不会用不相关的功能加入到我们的代码。

1.打开GameManager脚本

2.我们添加一个新的方法,我们创建的私有方法。

#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属性加载适当对应的房间。

这里有两件值得注意的事情,非常重要。

PhotonNetwork.LoadLevel()只能被调用,如果我们是主客户端。 我们首先检查我们使用PhotonNetwork.isMasterClient进行判断一下是否确定是主人。 其他加入房间的人者也有责任检查这一点,我们将在本节的下一部分中介绍,如何进行主客户端的判断。

我们使用PhotonNetwork.LoadLevel()加载我们想要的房间场景,我们不直接使用Unity自带的方法是因为我们希望依靠Photon在房间中的所有连接的客户端进行加载对应的场景,因为我们有了一个PhotonNetwork.automaticallySyncScene的方法,非常的方便实用。

现在我们有我们的方法来加载正确的场景,让我们玩家进行连接或者断开连接等功能已经实现。。

Watching Players Connection

目前,我们的GameManager脚本是一个常规的继承了MonoBehaviour,在教程前面部分我们学习了获得Photon回调的各种方法,现在GameManager需要玩家连接和断开连接。 我们来实现这个。

1.打开GameManager脚本

2.将基类从MonoBehaviour修改为Photon.PunBehaviour

public class GameManager : Photon.PunBehaviour {

3.我们添加以下Photon回调消息并保存GameManager脚本

#region Photon Messages

public override void OnPhotonPlayerConnected(PhotonPlayer other)

    Debug.Log("OnPhotonPlayerConnected() " + other.NickName); // 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.NickName); // seen when other disconnects

    if (PhotonNetwork.isMasterClient) 

    {

        Debug.Log("OnPhotonPlayerDisonnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected

        LoadArena();

    }

#endregion

3.保存脚本

现在,我们有一个完整的方法。 每当玩家加入或离开房间时,我们都会被通知,我们将调用上面刚刚构建的LoadArena()方法。 但是,如果我们是用PhotonNetwork.isMasterClient的主机,我们将调用LoadArena()。

我们现在回到大厅,可以根据对应的人数加入房间的同时加载正确的场景。

Loading Arena from the Lobby

1.编辑脚本启动器。

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.保存脚本

我们来测试一下,打开场景启动器,并运行它。点击“运行场景”,让项目连接并加入一个房间。就是这样,我们现可以进入一个房间。但如果你离开房间,你会注意到,当回到大厅,它会自动重新加入。

为什么会这样,分析日志,需要知道在哪里看,以及如何调试它。

1.运行启动场景

2.点击“运行场景”按钮,等待您加入房间,然后加载“1号房间”

3.清除Unity控制台无用的信息

4.点击“离开房间按钮”

5.学习使用Unity控制台,请注意,“DemoAnimator / Launcher:OnConnectedToMaster()被PUN调用”被记录

6.停止运行场景

7.双击日志条目“DemoAnimator / Launcher:OnConnectedToMaster()由PUN调用”脚本将被加载并指向调试调用的行。

8.所以,我们知道每当我们连接时,我们会自动加入一个JoinRandomRoom。但这不是我们想要的。

为了解决这个问题,我们需要注意,当用户点击“运行场景”按钮时,我们应该提出一个标志,注意显示出用户的连接过程。然后我们可以检查这个连接过程,以便在各种Photon回调中发现问题。

1.编辑脚本Launcher

2.在私有变量区域内创建一个新的属性

/// <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;

3.在OnConnectedToMaster()方法中,使用if语句进行判断PhotonNetwork.JoinRandomRoom()

// 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();

3.保存这个脚本

现在,如果我们再次测试并运行启动场景,并在大厅和游戏之间来进入退出,一切都很好,为了测试场景的自动同步,您需要发布出应用程序(发布桌面exe,它是运行测试的最快速度),并继续运行Unity的场景,有效率地连接和加入一个房间的两个玩家。如果Unity编辑器首先创建了房间,那么它将是MasterClient,您将能够在Unity Console中验证您在连接时获得“PhotonNetwork:Loading Level:1”和更高版本的“PhotonNetwork:Loading Level:2”发布的实例。

好!我们已经讲述了很多,但这只是demo的一半。我们需要解决玩家本身网络同步的问题,让我们在下一节中做。


不管你有关于Photon产品的问题或者是Photon价格问题或者Photon教程方面的问题或者其他问题都可以联系我们给我我们留言,我们真诚的为您服务。关注我们公众号PhotonServer获取等多新鲜资讯。


关注我们公众号PhotonServer 获取最新教程资源。

王者荣耀实时对战服务器Photon之Pun应用系列文章 如果你喜欢请关注我公众号,并推荐给你你的小伙伴,谢谢。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引