Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- void Start()
- {
- StartMatchMaker();
- }
- // NOTE: This is a workaround to avoid a disconnection bug (Issue Tracker case No. 794106)
- public override void OnClientDisconnect (NetworkConnection conn)
- {
- Debug.Log("Client would disconnect now if the following line is uncommented.");
- //base.OnClientDisconnect (conn);
- }
- 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());
- });
- }
- public void JoinGame(MatchDesc match)
- {
- 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...");
- }
- else
- {
- Debug.Log("Error Connecting!");
- }
- });
- }
- public void ListMatches()
- {
- if(matchMaker == null) StartMatchMaker();
- matchMaker.ListMatches(0, 20, "", OnMatchList);
- }
- public override void OnMatchList (UnityEngine.Networking.Match.ListMatchResponse matchList)
- {
- // Remove existing buttons
- DestroyButtons();
- 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
- {
- if(matchMaker != null)
- {
- 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);
- }
- void DestroyButtons()
- {
- foreach(Transform child in gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform)
- {
- DestroyImmediate(child.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement