Matchmaker

·  阅读 137

Unity的多玩家网络功能包含了玩家在因特网上互相玩而不需要公共IP地址的服务。用户可以创建游戏,获取活动游戏列表;加入并退出游戏。当在internet上玩时,网络流量将通过云中的Unity,而不是直接在客户端之间进行。这就避免了防火墙和NATs的问题,几乎可以在任何地方玩游戏。

在UnityEngine.Networking.Match名称空间里,unityengine的配对功能可以被用于特殊的脚本网络NetworkMatch。使用中继服务器的能力构建在LLAPI中,使它更容易使用。要使用它,从网络匹配中获得一个脚本并将其附加到一个管理器对象。下面是创建匹配、列出匹配和加入匹配的示例。\

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

public class HostGame : MonoBehaviour
{
    List<MatchDesc> matchList = new List<MatchDesc>();
    bool matchCreated;
    NetworkMatch networkMatch;

    void Awake()
    {
        networkMatch = gameObject.AddComponent<NetworkMatch>();
    }

    void OnGUI()
    {
        // You would normally not join a match you created yourself but this is possible here for demonstration purposes.
        if(GUILayout.Button("Create Room"))
        {
            CreateMatchRequest create = new CreateMatchRequest();
            create.name = "NewRoom";
            create.size = 4;
            create.advertise = true;
            create.password = "";

            networkMatch.CreateMatch(create, OnMatchCreate);
        }

        if (GUILayout.Button("List rooms"))
        {
            networkMatch.ListMatches(0, 20, "", OnMatchList);
        }

        if (matchList.Count > 0)
        {
            GUILayout.Label("Current rooms");
        }
        foreach (var match in matchList)
        {
            if (GUILayout.Button(match.name))
            {
                networkMatch.JoinMatch(match.networkId, "", OnMatchJoined);
            }
        }
    }

    public void OnMatchCreate(CreateMatchResponse matchResponse)
    {
        if (matchResponse.success)
        {
            Debug.Log("Create match succeeded");
            matchCreated = true;
            Utility.SetAccessTokenForNetwork(matchResponse.networkId, new NetworkAccessToken(matchResponse.accessTokenString));
            NetworkServer.Listen(new MatchInfo(matchResponse), 9000);
        }
        else
        {
            Debug.LogError ("Create match failed");
        }
    }

    public void OnMatchList(ListMatchResponse matchListResponse)
    {
        if (matchListResponse.success && matchListResponse.matches != null)
        {
            networkMatch.JoinMatch(matchListResponse.matches[0].networkId, "", OnMatchJoined);
        }
    }

    public void OnMatchJoined(JoinMatchResponse matchJoin)
    {
        if (matchJoin.success)
        {
            Debug.Log("Join match succeeded");
            if (matchCreated)
            {
                Debug.LogWarning("Match already set up, aborting...");
                return;
            }
            Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
            NetworkClient myClient = new NetworkClient();
            myClient.RegisterHandler(MsgType.Connect, OnConnected);
            myClient.Connect(new MatchInfo(matchJoin));
        }
        else
        {
            Debug.LogError("Join match failed");
        }
    }

    public void OnConnected(NetworkMessage msg)
    {
        Debug.Log("Connected!");
    }
}
复制代码


这个脚本设置了c指向公共统一的媒人服务器。它调用基类函数来创建、列出和连接匹配。CreateMatch创建一个匹配、连接匹配的匹配,以及在匹配服务器上注册的列表匹配。在内部,网络匹配使用web服务来建立匹配,在流程完成时调用给定的回调函数,如用于匹配创建的OnMatchCreate。\

填充单例NetworkMatch。matchSingleton告诉系统在连接到游戏时使用中继服务器而不是直接连接。因此,当客户端实际连接到游戏时,它会自动使用正确的中继服务器来匹配所选的匹配。\

分类:
后端
标签: