Advertisement
Munchy2007

CustomNetManager

May 9th, 2016
3,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Networking;
  4. using UnityEngine.Networking.Match;
  5. using UnityEngine.Networking.Types;
  6. using UnityEngine.UI;
  7.  
  8. public class MyNetworkManager : NetworkManager {
  9.     [SerializeField] GameObject gameBrowserWindow;
  10.     [SerializeField] GameObject noGamesText;
  11.     [SerializeField] BrowserButton browserButtonPrefab;
  12.     [SerializeField] GameObject offlineUI;
  13.     [SerializeField] GameObject onlineUI;
  14.     [HideInInspector] public bool isHost;
  15.     [HideInInspector] public long m_networkID;
  16.     [HideInInspector] public long m_nodeID;
  17.  
  18.     void Start()
  19.     {
  20.         StartMatchMaker();
  21.     }
  22.  
  23.     // NOTE: This is a workaround to avoid a disconnection bug (Issue Tracker case No. 794106)
  24.     public override void OnClientDisconnect (NetworkConnection conn)
  25.     {
  26.         Debug.Log("Client would disconnect now if the following line is uncommented.");
  27.         //base.OnClientDisconnect (conn);
  28.     }
  29.  
  30.     public void StartGameServer()
  31.     {
  32.         maxConnections = 2;
  33.         if(matchMaker == null) StartMatchMaker();
  34.         matchMaker.CreateMatch("My Game", (uint)8, true, "", info => {
  35.             base.OnMatchCreate (info);
  36.             isHost = true;
  37.             offlineUI.SetActive(false);
  38.             onlineUI.SetActive(true);
  39.             m_networkID = (long)info.networkId;
  40.             Debug.Log("Match Created " + m_networkID.ToString());
  41.         });
  42.     }
  43.        
  44.     public void JoinGame(MatchDesc match)
  45.     {
  46.         if(matchMaker == null) StartMatchMaker();
  47.         matchName = match.name;
  48.         matchSize = (uint)match.currentSize;
  49.         matchMaker.JoinMatch(match.networkId, "", info => {
  50.             base.OnMatchJoined(info);
  51.             if(info.success)
  52.             {
  53.                 m_nodeID = (long)info.nodeId;
  54.                 StartClient(new MatchInfo(info));
  55.                 offlineUI.SetActive(false);
  56.                 onlineUI.SetActive(true);
  57.                 Debug.Log("Match found, joining game...");
  58.             }
  59.             else
  60.             {
  61.                 Debug.Log("Error Connecting!");
  62.             }
  63.         });
  64.     }
  65.  
  66.     public void ListMatches()
  67.     {
  68.         if(matchMaker == null) StartMatchMaker();
  69.         matchMaker.ListMatches(0, 20, "", OnMatchList);
  70.     }
  71.  
  72.     public override void OnMatchList (UnityEngine.Networking.Match.ListMatchResponse matchList)
  73.     {
  74.         // Remove existing buttons
  75.         DestroyButtons();
  76.  
  77.         if(matchList.matches == null || matchList.matches.Count == 0)
  78.         {
  79.             noGamesText.SetActive(true);
  80.         }
  81.         else
  82.         {
  83.             noGamesText.SetActive(false);
  84.             foreach(var match in matchList.matches)
  85.             {
  86.                 var button = Instantiate<BrowserButton>(browserButtonPrefab);
  87.                 button.OnClickedHandler = JoinGame;
  88.                 button.match = match;
  89.                 button.SetText(match.name);
  90.                 button.transform.SetParent(gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform, false);
  91.             }
  92.         }
  93.     }
  94.  
  95.     public void Disconnect()
  96.     {
  97.         if(isHost)
  98.         {
  99.             matchMaker.DestroyMatch((NetworkID)m_networkID,response =>
  100.                 {
  101.                     Debug.Log("Shutdown host");
  102.                     NetworkManager.singleton.StopHost();
  103.                     NetworkManager.singleton.StopMatchMaker();
  104.                 });
  105.         }
  106.         else
  107.         {
  108.             if(matchMaker != null)
  109.             {
  110.                 matchMaker.DropConnection((NetworkID)m_networkID, (NodeID)m_nodeID, response =>
  111.                     {
  112.                         Debug.Log("Shutdown client");
  113.                         NetworkManager.singleton.StopClient();
  114.                         NetworkManager.singleton.StopMatchMaker();
  115.                     });
  116.             }
  117.  
  118.         }
  119.  
  120.         offlineUI.SetActive(true);
  121.         onlineUI.SetActive(false);
  122.     }
  123.  
  124.     void DestroyButtons()
  125.     {
  126.         foreach(Transform child in gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform)
  127.         {
  128.             DestroyImmediate(child.gameObject);
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement