Advertisement
chrisall76

Untitled

Nov 10th, 2015
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.84 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Networking;
  4. using UnityEngine.Networking.Types;
  5. using UnityEngine.Networking.Match;
  6. using System.Collections;
  7.  
  8.  
  9. namespace UnityStandardAssets.Network
  10. {
  11.     public class LobbyManager : NetworkLobbyManager
  12.     {
  13.         static public LobbyManager s_Singleton;
  14.  
  15.         [Tooltip("The minimum number of players in the lobby before player can be ready")]
  16.         public int minPlayer;
  17.  
  18.         public LobbyTopPanel topPanel;
  19.  
  20.         public RectTransform mainMenuPanel;
  21.         public RectTransform lobbyPanel;
  22.  
  23.         public LobbyInfoPanel infoPanel;
  24.  
  25.         protected RectTransform currentPanel;
  26.  
  27.         public Button backButton;
  28.  
  29.         public Text statusInfo;
  30.         public Text hostInfo;
  31.  
  32.         //used to disconnect a client properly when exiting the matchmaker
  33.         public bool isMatchmaking = false;
  34.         protected bool _disconnectServer = false;
  35.        
  36.         protected System.UInt64 _currentMatchID;
  37.  
  38.         protected UnityStandardAssets.Network.LobbyHook _lobbyHooks;
  39.  
  40.         void Start(){
  41.             s_Singleton = this;
  42.             _lobbyHooks = GetComponent<UnityStandardAssets.Network.LobbyHook>();
  43.             currentPanel = mainMenuPanel;
  44.  
  45.             backButton.gameObject.SetActive(false);
  46.             GetComponent<Canvas>().enabled = true;
  47.  
  48.             DontDestroyOnLoad(gameObject);
  49.  
  50.             SetServerInfo("Offline", "None");
  51.         }
  52.  
  53.         public override void OnLobbyClientSceneChanged(NetworkConnection conn)
  54.         {
  55.             if (!conn.playerControllers[0].unetView.isLocalPlayer)
  56.                 return;
  57.  
  58.             if (Application.loadedLevelName == lobbyScene)
  59.             {
  60.                 if (topPanel.isInGame)
  61.                 {
  62.                     ChangeTo(lobbyPanel);
  63.                     if (isMatchmaking)
  64.                     {
  65.                         if (conn.playerControllers[0].unetView.isServer)
  66.                         {
  67.                             backDelegate = StopHostClbk;
  68.                         }
  69.                         else
  70.                         {
  71.                             backDelegate = StopClientClbk;
  72.                         }
  73.                     }
  74.                     else
  75.                     {
  76.                         if (conn.playerControllers[0].unetView.isClient)
  77.                         {
  78.                             backDelegate = StopHostClbk;
  79.                         }
  80.                         else
  81.                         {
  82.                             backDelegate = StopClientClbk;
  83.                         }
  84.                     }
  85.                 }
  86.                 else
  87.                 {
  88.                     ChangeTo(mainMenuPanel);
  89.                 }
  90.  
  91.                 topPanel.ToggleVisibility(true);
  92.                 topPanel.isInGame = false;
  93.             }
  94.             else
  95.             {
  96.                 ChangeTo(null);
  97.  
  98.                 Destroy(GameObject.Find("MainMenuUI(Clone)"));
  99.  
  100.                 backDelegate = StopGameClbk;
  101.                 topPanel.isInGame = true;
  102.                 topPanel.ToggleVisibility(false);
  103.             }
  104.         }
  105.  
  106.         public void ChangeTo(RectTransform newPanel)
  107.         {
  108.             if (currentPanel != null)
  109.             {
  110.                 currentPanel.gameObject.SetActive(false);
  111.             }
  112.  
  113.             if (newPanel != null)
  114.             {
  115.                 newPanel.gameObject.SetActive(true);
  116.             }
  117.  
  118.             currentPanel = newPanel;
  119.  
  120.             if (currentPanel != mainMenuPanel)
  121.             {
  122.                 backButton.gameObject.SetActive(true);
  123.             }
  124.             else
  125.             {
  126.                 backButton.gameObject.SetActive(false);
  127.                 SetServerInfo("Offline", "None");
  128.                 isMatchmaking = false;
  129.             }
  130.         }
  131.  
  132.         public void DisplayIsConnecting()
  133.         {
  134.             var _this = this;
  135.             infoPanel.Display("Connecting...", "Cancel", () => { _this.backDelegate(); });
  136.         }
  137.  
  138.         public void SetServerInfo(string status, string host)
  139.         {
  140.             statusInfo.text = status;
  141.             hostInfo.text = host;
  142.         }
  143.  
  144.  
  145.         public delegate void BackButtonDelegate();
  146.         public BackButtonDelegate backDelegate;
  147.         public void GoBackButton()
  148.         {
  149.             backDelegate();
  150.         }
  151.  
  152.         // ----------------- Server management
  153.  
  154.         public void SimpleBackClbk()
  155.         {
  156.             ChangeTo(mainMenuPanel);
  157.         }
  158.  
  159.         public void StopHostClbk()
  160.         {
  161.             if (isMatchmaking)
  162.             {
  163.                 this.matchMaker.DestroyMatch((NetworkID)_currentMatchID, OnMatchDestroyed);
  164.                 _disconnectServer = true;
  165.             }
  166.             else
  167.             {
  168.                 StopHost();
  169.             }
  170.  
  171.            
  172.             ChangeTo(mainMenuPanel);
  173.         }
  174.  
  175.         public void StopClientClbk()
  176.         {
  177.             StopClient();
  178.  
  179.             if (isMatchmaking)
  180.             {
  181.                 StopMatchMaker();
  182.             }
  183.  
  184.             ChangeTo(mainMenuPanel);
  185.         }
  186.  
  187.         public void StopServerClbk()
  188.         {
  189.             StopServer();
  190.             ChangeTo(mainMenuPanel);
  191.         }
  192.  
  193.         public void StopGameClbk()
  194.         {
  195.             SendReturnToLobby();
  196.             ChangeTo(lobbyPanel);
  197.         }
  198.  
  199.         //===================
  200.  
  201.         public override void OnStartHost()
  202.         {
  203.             base.OnStartHost();
  204.  
  205.             ChangeTo(lobbyPanel);
  206.             backDelegate = StopHostClbk;
  207.             SetServerInfo("Hosting", networkAddress);
  208.         }
  209.  
  210.         public override void OnClientConnect(NetworkConnection conn)
  211.         {
  212.             Debug.Log("Client Connected!");
  213.             base.OnClientConnect(conn);
  214.  
  215.             infoPanel.gameObject.SetActive(false);
  216.  
  217.             if (!NetworkServer.active)
  218.             {//only to do on pure client (not self hosting client)
  219.                 ChangeTo(lobbyPanel);
  220.                 backDelegate = StopClientClbk;
  221.                 SetServerInfo("Client", networkAddress);
  222.             }
  223.         }
  224.  
  225.         public override void OnMatchCreate(UnityEngine.Networking.Match.CreateMatchResponse matchInfo)
  226.         {
  227.             base.OnMatchCreate(matchInfo);
  228.  
  229.             _currentMatchID = (System.UInt64)matchInfo.networkId;
  230.         }
  231.  
  232.         public void OnMatchDestroyed(BasicResponse resp)
  233.         {
  234.             if (_disconnectServer)
  235.             {
  236.                 StopMatchMaker();
  237.                 StopHost();
  238.             }
  239.         }
  240.  
  241.         // ----------------- Server callbacks ------------------
  242.  
  243.         //we want to disable the button JOIN if we don't have enough player
  244.         //But OnLobbyClientConnect isn't called on hosting player. So we override the lobbyPlayer creation
  245.         public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
  246.         {
  247.             Debug.Log("Create Lobby Player!");
  248.             GameObject obj = Instantiate(lobbyPlayerPrefab.gameObject) as GameObject;
  249.  
  250.             LobbyPlayer newPlayer = obj.GetComponent<LobbyPlayer>();
  251.             newPlayer.RpcToggleJoinButton(numPlayers + 1 >= minPlayer); ;
  252.  
  253.             for (int i = 0; i < numPlayers; ++i)
  254.             {
  255.                 LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
  256.  
  257.                 if (p != null)
  258.                 {
  259.                     p.RpcToggleJoinButton(numPlayers + 1 >= minPlayer);
  260.                 }
  261.             }
  262.  
  263.             return obj;
  264.         }
  265.  
  266.         public override void OnLobbyServerDisconnect(NetworkConnection conn)
  267.         {
  268.             for (int i = 0; i < numPlayers; ++i)
  269.             {
  270.                 LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
  271.  
  272.                 if (p != null)
  273.                 {
  274.                     p.RpcToggleJoinButton(numPlayers >= minPlayer);
  275.                 }
  276.             }
  277.  
  278.         }
  279.  
  280.         public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
  281.         {
  282.             //This hook allows you to apply state data from the lobby-player to the game-player
  283.             //just subclass "LobbyHook" and add it to the lobby object.
  284.  
  285.             if (_lobbyHooks)
  286.                 _lobbyHooks.OnLobbyServerSceneLoadedForPlayer(this, lobbyPlayer, gamePlayer);
  287.  
  288.             return true;
  289.         }
  290.  
  291.         // ----------------- Client callbacks ------------------
  292.  
  293.         public override void OnClientDisconnect(NetworkConnection conn)
  294.         {
  295.             base.OnClientDisconnect(conn);
  296.             ChangeTo(mainMenuPanel);
  297.         }
  298.  
  299.         public override void OnClientError(NetworkConnection conn, int errorCode)
  300.         {
  301.             ChangeTo(mainMenuPanel);
  302.             infoPanel.Display("Cient error : " + (errorCode == 6 ? "timeout" : errorCode.ToString()), "Close", null);
  303.         }
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement