0

由于价格原因决定通过 Unity over Photon 进行联网(如果有更好的联网方式,请告诉我)我一直在尝试创建一个简单的统一匹配器,当用户进入游戏时,他们首先会寻找任何其他匹配( AKA 房间/游戏),如果有,他们会加入,如果没有,他们会创建一个。但是,我正在努力寻找如何使用统一媒人的明确解释,因此我可以开发代码来获得我想要的行为,这似乎更棘手,因为在最近的更新中统一似乎正在改变它的网络系统,所以我相信之前的任何事情5.4 目前已过时。我发现的唯一有用的东西是这个示例代码,但我想更多地了解系统,所以我不会错过任何关键概念,这可能是因为我相当初学者。

所以我今天要问你的是,你能指导我清楚地解释与当前统一相关的匹配系统(包括脚本)吗?将作为像我这样的其他人未来的参考。

感谢您的阅读,当然,任何答案将不胜感激。

[我在 C# 中工作]

编辑:

我知道我的问题很难回答,所以我将测试示例代码,直到我得到所需的行为并发布任何我无法解决的错误。

问题 1

  1. 我已将确切的代码(包括在下面以便于参考)复制到我附加到我的网络管理器的脚本中。但是由于保护级别,我遇到了错误-

在此处输入图像描述

我有两个关于如何解决这个问题的理论 -

  1. 更改方法访问器
  2. 不从单一行为派生脚本

我目前正在测试它们,任何关于如何解决这个问题的建议都会非常有用。我想知道我是否应该将此报告给统一,因为对我来说,这似乎是统一文档中的错误示例,它说它已针对 5.4 进行了更新。谢谢。

问题 2

我已经创建了这个脚本并在附加到带有单个网络管理器的空 GameObject 的编辑器中运行它,我没有收到编译器错误,但是当我运行它时调试“错误:匹配搜索失败”,这意味着 listmatches 功能不成功,我的脚本在下面 -

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
using System.Collections.Generic;

public class MyMatchMaker : MonoBehaviour {
    bool done;
    // Use this for initialization
    void Start () {
        NetworkManager.singleton.StartMatchMaker();
        NetworkManager.singleton.matchMaker.ListMatches(0, 20, "Match", false, 0, 1, OnMatchList);
        Debug.Log("Searching");
    }

    public virtual void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
    {
        if (success)
        {
            if (matchList.Count != 0)
            {
                Debug.Log("Matches Found");
                NetworkManager.singleton.matchMaker.JoinMatch(matchList[0].networkId, "", "", "", 0, 1, OnMatchJoined);
            }
            else
            {
                Debug.Log("No Matches Found");
                Debug.Log("Creating Match");
                NetworkManager.singleton.matchMaker.CreateMatch("Match", 2, true, "", "", "", 0, 1, OnMatchCreate);
            }
        }
        else
        {
            Debug.Log("ERROR : Match Search Failure");
        }
    }

    public virtual void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Joined");
            MatchInfo hostInfo = matchInfo;
            NetworkManager.singleton.StartClient(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Join Failure");
        }

    }

    public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Created");

            MatchInfo hostInfo = matchInfo;
            NetworkServer.Listen(hostInfo, 9000);
            NetworkManager.singleton.StartHost(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Create Failure");
        }
    }

    void OnConnect ()
    {
        if (Network.isServer)
        {
            Debug.Log("You are Server");
        }
        else if (Network.isClient)
        {
            Debug.Log("You are Client");
        }
        else
        {
            Debug.Log("ERROR : MatchMaking Failed, Peer type is neither Client nor Server");
        }
    }
}

谢谢,希望你能看到我哪里出错了。

@程序员

4

1 回答 1

1

啊...让这个代码工作我调试了回调中的信息字符串,并且不得不更改一些项目设置。

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
using System.Collections.Generic;

public class MyMatchMaker : MonoBehaviour {
    bool done;
    // Use this for initialization
    void Start () {
        NetworkManager.singleton.StartMatchMaker();
        NetworkManager.singleton.matchMaker.ListMatches(0, 20, "Match", false, 0, 1, OnMatchList);
        Debug.Log("Searching");
    }

    public virtual void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
    {
        if (success)
        {
            if (matchList.Count != 0)
            {
                Debug.Log("Matches Found");
                NetworkManager.singleton.matchMaker.JoinMatch(matchList[0].networkId, "", "", "", 0, 1, OnMatchJoined);
            }
            else
            {
                Debug.Log("No Matches Found");
                Debug.Log("Creating Match");
                NetworkManager.singleton.matchMaker.CreateMatch("Match", 2, true, "", "", "", 0, 1, OnMatchCreate);
            }
        }
        else
        {
            Debug.Log("ERROR : Match Search Failure");
        }
    }

    public virtual void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Joined");
            MatchInfo hostInfo = matchInfo;
            NetworkManager.singleton.StartClient(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Join Failure");
        }

    }

    public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Created");

            MatchInfo hostInfo = matchInfo;
            NetworkServer.Listen(hostInfo, 9000);
            NetworkManager.singleton.StartHost(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Create Failure");
        }
    }
}
于 2016-08-31T20:40:24.897 回答