Hi all,
I’ve been playing around with the Unity Match Making system, and whilst I have managed to start a match and have a client join, the client always disconnects after about 15 seconds. There is no error message on either server or client, the only indication I get is that the matchmaker property on the client networkmanager becomes null.
Would anyone care to take a look at my network manager script to see if I’ve made any fundamental errors? Otherwise I think I’ll file it as a bug.
This is happening with just a single client connected to the host, that has a basic script attached that just slowly spins it on its Y axis. NetworkTransform component update rate is set to 9 (the default).
Thanks
NetworkManager Code
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using UnityEngine.UI;
public class MyNetworkManager : NetworkManager {
[SerializeField] GameObject gameBrowserWindow;
[SerializeField] GameObject noGamesText;
[SerializeField] BrowserButton browserButtonPrefab;
[SerializeField] GameObject offlineUI;
[SerializeField] GameObject onlineUI;
[HideInInspector] public bool isHost;
[HideInInspector] public long m_networkID;
[HideInInspector] public long m_nodeID;
public NetworkMatch TheMatchMaker; // Used to visually track the existance of the NetworkMatch in the inspector - added for debugging purposes only
void Start()
{
StartMatchMaker();
StartCoroutine("AutoListGames");
}
public void StartGameServer()
{
maxConnections = 2;
if(matchMaker == null) StartMatchMaker();
matchMaker.CreateMatch("My Game", (uint)8, true, "", info => {
base.OnMatchCreate (info);
isHost = true;
offlineUI.SetActive(false);
onlineUI.SetActive(true);
m_networkID = (long)info.networkId;
Debug.Log("Match Created " + m_networkID.ToString());
StopCoroutine("AutoListGames");
});
}
public void JoinGame(MatchDesc match)
{
TheMatchMaker = matchMaker;
if(matchMaker == null) StartMatchMaker();
matchName = match.name;
matchSize = (uint)match.currentSize;
matchMaker.JoinMatch(match.networkId, "", info => {
base.OnMatchJoined(info);
if(info.success)
{
m_nodeID = (long)info.nodeId;
StartClient(new MatchInfo(info));
offlineUI.SetActive(false);
onlineUI.SetActive(true);
Debug.Log("Match found, joining game...");
StopCoroutine("AutoListGames");
}
else
{
Debug.Log("Error Connecting!");
}
});
}
IEnumerator AutoListGames()
{
while(true)
{
yield return new WaitForSeconds(3f);
if(gameBrowserWindow.gameObject.activeInHierarchy)
{
if(matchMaker == null) StartMatchMaker();
matchMaker.ListMatches(0, 20, "", OnMatchList);
}
}
}
public override void OnMatchList (UnityEngine.Networking.Match.ListMatchResponse matchList)
{
// Remove existing buttons
foreach(Transform child in gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform)
{
DestroyImmediate(child.gameObject);
}
if(matchList.matches == null || matchList.matches.Count == 0)
{
noGamesText.SetActive(true);
}
else
{
noGamesText.SetActive(false);
foreach(var match in matchList.matches)
{
var button = Instantiate<BrowserButton>(browserButtonPrefab);
button.OnClickedHandler = JoinGame;
button.match = match;
button.SetText(match.name);
button.transform.SetParent(gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform, false);
}
}
}
public void Disconnect()
{
if(isHost)
{
matchMaker.DestroyMatch((NetworkID)m_networkID,response =>
{
Debug.Log("Shutdown host");
NetworkManager.singleton.StopHost();
NetworkManager.singleton.StopMatchMaker();
});
}
else
{
matchMaker.DropConnection((NetworkID)m_networkID, (NodeID)m_nodeID, response =>
{
Debug.Log("Shutdown client");
NetworkManager.singleton.StopClient();
NetworkManager.singleton.StopMatchMaker();
});
}
offlineUI.SetActive(true);
onlineUI.SetActive(false);
StartCoroutine("AutoListGames");
}
}
Player script
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Player : NetworkBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(isLocalPlayer)
{
transform.Rotate(0,20 * Time.deltaTime,0);
}
}
}
BrowserButton script
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.UI;
public class BrowserButton : MonoBehaviour, IPointerClickHandler {
public System.Action<MatchDesc> OnClickedHandler;
public MatchDesc match;
public void SetText(string gameName)
{
GetComponentInChildren<Text>().text = gameName;
}
#region IPointerClickHandler implementation
public void OnPointerClick (PointerEventData eventData)
{
if(OnClickedHandler != null)
OnClickedHandler.Invoke(match);
}
#endregion
}