网络大厅经理

时间:2018-04-09 18:41:08

标签: unity3d multiplayer

我正在使用unity3D开发一个多人在线应用程序,我正在使用networkLobbyManager,我设法创建一个匹配并允许玩家通过运行matchMaker.JoinMatch加入比赛,所以我拥有所有“Lobby Scene”中的玩家,但我不明白我们如何加入他们的“游戏场景”。 (我发现只有当所有玩家都准备就绪时我们才能进入场景,但我不明白我怎么能把它们都准备好了)。我的代码:

public InputField impF;
public InputField impF1;
public Dropdown d;
List<MatchInfoSnapshot> m;
MatchInfoSnapshot ma;
public int n = 1;

// Use this for initialization
public void enableMatch()
{
    Debug.Log("@ MMStart ");
    this.StartMatchMaker();
}

public override void OnStartHost()
{
    base.OnStartHost();
    Debug.Log("the game has created");
}

public void crereMatch()
{
    string nom = impF.text;
    Debug.Log(nom);
    MMCreateMateches(nom);
    n = 2;
}

public void findMtches()
{
    MMListMateches();
}

public void joinMatch()
{
    Debug.Log(d.options[d.value].text);
    foreach (var match in this.matches)
    {
        Debug.Log("dans la boucle des matches");
        Debug.Log(match.name);
        if ((d.options[d.value].text).Equals(match.name))
        {
            //on récupere 
            ma = match;
            Debug.Log("dans le if " + ma.name);
            break;
        }
    }
    // this.f
    m = this.matches;
    ma = m.Find(x => (x.name).Equals(ma.name));
    Debug.Log("apres recuperation de la liste" + ma.name);
    MMjoinMateches(ma);
}

void MMListMateches()
{
    Debug.Log("@ MMListMateches ");
    this.matchMaker.ListMatches(0, 20, "", true, 0, 0, OnMatchList);
}

public override void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
{
    Debug.Log("@ OnMatchList ");
    base.OnMatchList(success, extendedInfo, matchList);
    if (!success)
    {
        Debug.Log("liste failed " + extendedInfo);
    }
    else
    {
        if (matches.Count > 0)
        {  //les matches en cours > 0 ms on doit le joiner 
            Debug.Log("succesfully listed match 1 er match :" + matchList[0]);
            Debug.Log(matchList[0].name);
            foreach (var match in this.matches)
            {
                Debug.Log("dans la boucle");
                Debug.Log(match.name);
                Debug.Log(" wééééééééééééééééééé ");
            }
            d.ClearOptions();
            foreach (var match in this.matches)
            {
                Debug.Log(match.name);
                // impF1.text = match.name;
                List<string> m_DropOptions = new List<string> { match.name };

                d.AddOptions(m_DropOptions);
            }
            //   MMjoinMateches(matchList[0]);
        }
        else
        {
            Debug.Log("no mateches");
            // MMCreateMateches();
        }
    }
}

void MMjoinMateches(MatchInfoSnapshot firstMatch)
{
    Debug.Log("@ MMjoinMateches ");
    this.matchMaker.JoinMatch(firstMatch.networkId, "", "", "", 0, 0, OnMatchJoined);
}

public override void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
{
    Debug.Log("@ OnMatchJoined ");
    base.OnMatchJoined(success, extendedInfo, matchInfo);
    if (!success)
    {
        Debug.Log("failed to join match " + extendedInfo);
        impF1.text = "failed";
    }
    else
    {
        Debug.Log("succesfuly joined match " + matchInfo.networkId);
        impF1.text = "succes";
        this.StartClient(matchInfo);
    }
}

void MMCreateMateches(string nom)
{
    Debug.Log("@ MMCreateMateches ");
    Debug.Log(nom);
    this.matchMaker.CreateMatch(nom, 15, true, "", "", "", 0, 0, OnMatchCreate);
}

public override void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
    Debug.Log("@ OnMatchCreate ");
    base.OnMatchCreate(success, extendedInfo, matchInfo);
    if (!success)
    {

        Debug.Log("failed to create match " + extendedInfo);
    }
    else
    {
        Debug.Log("succesfuly created match " + matchInfo.networkId);
        OnStartHost();
    }
}

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

1 个答案:

答案 0 :(得分:0)

观看Holistic 3D的本教程,了解如何设置Unity Network Lobb y(在资源商店中免费)。 https://www.youtube.com/watch?v=jklWlm5v21k

在设置中,确定进入房间所需的Max Players。您可以将其设置为“ 1”,以便任何人都可以随时输入。

将最大玩家数从4更改为1 See Link