Search Unity

Matchmaker Example?

Discussion in 'Multiplayer' started by Deleted User, Jul 8, 2015.

  1. Deleted User

    Deleted User

    Guest

    Anyone know of a tutorial or project that has a working matchmaker? The sample projects pinned to this thread pretty much all have internal errors just when loading them up, or I would try to get it working with one of them....
     
  2. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    This is a modified version of an example I found earlier. It covers most of the basics of the matchmaker service

    Code (CSharp):
    1.  
    2.  
    3.  
    4. public enum ConnectionType
    5. {
    6.     Disconnected,
    7.     Client,
    8.     Server,
    9.     Host
    10. }
    11.  
    12.     void Start()
    13.     {
    14.         StartMatchMaker();
    15.         RefreshServerList();
    16.     }
    17.  
    18.     public static ConnectionType connectionType = ConnectionType.Disconnected;
    19.  
    20.     public void RefreshServerList()
    21.     {
    22.         matchMaker.ListMatches(0, 20, "", OnMatchList);
    23.     }
    24.  
    25.     public override void OnMatchList(ListMatchResponse matchListResponse)
    26.     {
    27.         if (matchListResponse.success && matchListResponse.matches != null)
    28.         {
    29.             serverBrowser.ClearServerInfo();
    30.             //insert code to display match list
    31.         }
    32.     }
    33.     public override void OnMatchCreate(CreateMatchResponse matchResponse)
    34.     {
    35.         if (matchResponse.success)
    36.         {
    37.             Debug.Log("Create match succeeded");
    38.             Utility.SetAccessTokenForNetwork(matchResponse.networkId, new NetworkAccessToken(matchResponse.accessTokenString));
    39.             NetworkServer.Listen(new MatchInfo(matchResponse), 9000);
    40.             if (connectionType == ConnectionType.Host)
    41.                 StartHost(matchInfo);
    42.             else
    43.                 StartServer(matchInfo);
    44.         }
    45.         else
    46.         {
    47.             Debug.LogError("Create match failed");
    48.         }
    49.     }
    50.  
    51.     public void CreateMatch()
    52.     {
    53.             CreateMatchRequest create = new CreateMatchRequest();
    54.             create.name = "NewRoom";
    55.             create.size = 8;
    56.             create.advertise = true;
    57.             create.password = "";
    58.  
    59.             matchMaker.CreateMatch(create, OnMatchCreate);
    60.     }
    61.     public void OnMatchJoined(JoinMatchResponse matchJoin)
    62.     {
    63.         if (matchJoin.success)
    64.         {
    65.             Debug.Log("Join match succeeded");
    66.             if (matchCreated)
    67.             {
    68.                 Debug.LogWarning("Match already set up, aborting...");
    69.                 return;
    70.             }
    71.             Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
    72.             NetworkClient myClient = new NetworkClient();
    73.             myClient.RegisterHandler(MsgType.Connect, OnMatchConnected);
    74.             myClient.Connect(new MatchInfo(matchJoin));
    75.             StartClient(matchInfo);
    76.         }
    77.         else
    78.         {
    79.             Debug.LogError("Join match failed");
    80.             isClient = false;
    81.         }
    82.     }
    83.  
    84.     public void JoinMatch(MatchInfo match)
    85. {
    86.     matchMaker.JoinMatch(match.networkId, "", OnMatchJoined);
    87. }
    88.  
    89.  
     
    Last edited: Jul 9, 2015