Hi,
I believe I found an out-of-date example in the unity docs, as I am fairly beginner-ish to unity I have been looking for basic matchmaking code which i could adapt. I found the example here and I have since copied it into a script to try get it working but I am getting errors which I believe are due to CreateMatchRequest() being out-of-date (I’m sure there is some better code term). Hence I have tried to look it up and have found nothing in the unity 5.4 docs but I did find it in the unity 5.3 docs (here). So I believe the unity example in the NetworkMatch class documentation is out-of-date.
Thanks this allowed me to adapt the script as below -
Original Script (5.3)
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
public class SimpleMatchMaker : MonoBehaviour
{
void Start()
{
NetworkManager.singleton.StartMatchMaker();
}
//call this method to request a match to be created on the server
public void CreateInternetMatch(string matchName)
{
CreateMatchRequest create = new CreateMatchRequest();
create.name = matchName;
create.size = 4;
create.advertise = true;
create.password = "";
NetworkManager.singleton.matchMaker.CreateMatch(create, OnInternetMatchCreate);
}
//this method is called when your request for creating a match is returned
private void OnInternetMatchCreate(CreateMatchResponse matchResponse)
{
if (matchResponse != null && matchResponse.success)
{
//Debug.Log("Create match succeeded");
MatchInfo hostInfo = new MatchInfo(matchResponse);
NetworkServer.Listen(hostInfo, 9000);
NetworkManager.singleton.StartHost(hostInfo);
}
else
{
Debug.LogError("Create match failed");
}
}
//call this method to find a match through the matchmaker
public void FindInternetMatch(string matchName)
{
NetworkManager.singleton.matchMaker.ListMatches(0, 20, matchName, OnInternetMatchList);
}
//this method is called when a list of matches is returned
private void OnInternetMatchList(ListMatchResponse matchListResponse)
{
if (matchListResponse.success)
{
if (matchListResponse.matches.Count != 0)
{
//Debug.Log("A list of matches was returned");
//join the last server (just in case there are two...)
NetworkManager.singleton.matchMaker.JoinMatch(matchListResponse.matches[matchListResponse.matches.Count - 1].networkId, "", OnJoinInternetMatch);
}
else
{
Debug.Log("No matches in requested room!");
}
}
else
{
Debug.LogError("Couldn't connect to match maker");
}
}
//this method is called when your request to join a match is returned
private void OnJoinInternetMatch(JoinMatchResponse matchJoin)
{
if (matchJoin.success)
{
//Debug.Log("Able to join a match");
MatchInfo hostInfo = new MatchInfo(matchJoin);
NetworkManager.singleton.StartClient(hostInfo);
}
else
{
Debug.LogError("Join match failed");
}
}
}
Adapted Script (5.4)
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
using System.Collections.Generic;
public class MatchMaker : MonoBehaviour
{
void Start()
{
NetworkManager.singleton.StartMatchMaker();
}
//call this method to request a match to be created on the server
public void CreateInternetMatch(string matchName)
{
NetworkManager.singleton.matchMaker.CreateMatch(matchName,2, true, "", "", "", 0, 1, OnMatchCreate);
}
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);
}
else
{
Debug.LogError("Create match failed");
}
}
//call this method to find a match through the matchmaker
public void FindInternetMatch(string matchName)
{
NetworkManager.singleton.matchMaker.ListMatches(0, 20, matchName, false, 0, 1, OnMatchList);
}
public virtual void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
{
if (success)
{
if (matchList.Count != 0)
{
//Debug.Log("A list of matches was returned");
//join the last server (just in case there are two...)
NetworkManager.singleton.matchMaker.JoinMatch(matchList[matchList.Count - 1].networkId, "","", "", 0, 1, OnMatchJoined);
}
else
{
Debug.Log("No matches in requested room!");
}
}
}
public virtual void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
//Debug.Log("Able to join a match");
MatchInfo hostInfo = matchInfo;
NetworkManager.singleton.StartClient(hostInfo);
}
else
{
Debug.LogError("Join match failed");
}
}
}