- Home /
Looking for basic explanation on unity matchmaker (5.4) and how to use in scripts
Having decided to do Networking through Unity over Photon due to price (if there is a better way of networking let me know) I have been trying to create a simple unity matchmaker where when a user enters the game they first look for any other match (AKA rooms / games), if there is one they join it, if there isn't one they create one. However I am struggling to find a clear explanation of how to use the unity matchmaker, so I can develop code to get my desired behavior which seems even trickier as it seems unity is changing it's networking system a lot in recent updates so I believe anything before 5.4 is currently out-of-date. The only useful thing I have found is this example code but I want to understand the system more so I don't miss any key concepts, which is likely as I am fairly beginner-ish.
So what I am asking you today is can you direct me to a clear explanation of the matchmaking system (including scripting) which is relevant to the current unity or, admittedly much harder and more time consuming, could you try to do it yourself and it will serve as future reference for others like me.
Thanks for reading, of course any answers will be greatly appreciated.
[i work in c#]
EDIT:
I understand my question is very difficult to answer so I am going to test out the example code until i get the desired behavior and post any errors which i cannot solve.
I have copied the exact code (Included below for easier reference) into a script which i have attached to my network manager. But I am getting errors due to protection levels -
I have two theories on how to solve this -
Changing the method accessors
Not deriving the script from monobehaviour
I am currently testing them out, any advice on how to solve this would be very useful. I wonder if I should report this to unity as to me it seems to be a wrong example in unity docs which say it is updated for 5.4. Thanks.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
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)
{
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");
}
}
}
Your answer
Follow this Question
Related Questions
How can i spawn my player in matchmaker using my UI? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers