Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using UnityEngine.Networking.Types;
- using UnityEngine.Networking.Match;
- using System.Collections;
- namespace UnityStandardAssets.Network
- {
- public class LobbyManager : NetworkLobbyManager
- {
- static public LobbyManager s_Singleton;
- [Tooltip("The minimum number of players in the lobby before player can be ready")]
- public int minPlayer;
- public LobbyTopPanel topPanel;
- public RectTransform mainMenuPanel;
- public RectTransform lobbyPanel;
- public LobbyInfoPanel infoPanel;
- protected RectTransform currentPanel;
- public Button backButton;
- public Text statusInfo;
- public Text hostInfo;
- //used to disconnect a client properly when exiting the matchmaker
- public bool isMatchmaking = false;
- protected bool _disconnectServer = false;
- protected System.UInt64 _currentMatchID;
- protected UnityStandardAssets.Network.LobbyHook _lobbyHooks;
- void Start(){
- s_Singleton = this;
- _lobbyHooks = GetComponent<UnityStandardAssets.Network.LobbyHook>();
- currentPanel = mainMenuPanel;
- backButton.gameObject.SetActive(false);
- GetComponent<Canvas>().enabled = true;
- DontDestroyOnLoad(gameObject);
- SetServerInfo("Offline", "None");
- }
- public override void OnLobbyClientSceneChanged(NetworkConnection conn)
- {
- if (!conn.playerControllers[0].unetView.isLocalPlayer)
- return;
- if (Application.loadedLevelName == lobbyScene)
- {
- if (topPanel.isInGame)
- {
- ChangeTo(lobbyPanel);
- if (isMatchmaking)
- {
- if (conn.playerControllers[0].unetView.isServer)
- {
- backDelegate = StopHostClbk;
- }
- else
- {
- backDelegate = StopClientClbk;
- }
- }
- else
- {
- if (conn.playerControllers[0].unetView.isClient)
- {
- backDelegate = StopHostClbk;
- }
- else
- {
- backDelegate = StopClientClbk;
- }
- }
- }
- else
- {
- ChangeTo(mainMenuPanel);
- }
- topPanel.ToggleVisibility(true);
- topPanel.isInGame = false;
- }
- else
- {
- ChangeTo(null);
- Destroy(GameObject.Find("MainMenuUI(Clone)"));
- backDelegate = StopGameClbk;
- topPanel.isInGame = true;
- topPanel.ToggleVisibility(false);
- }
- }
- public void ChangeTo(RectTransform newPanel)
- {
- if (currentPanel != null)
- {
- currentPanel.gameObject.SetActive(false);
- }
- if (newPanel != null)
- {
- newPanel.gameObject.SetActive(true);
- }
- currentPanel = newPanel;
- if (currentPanel != mainMenuPanel)
- {
- backButton.gameObject.SetActive(true);
- }
- else
- {
- backButton.gameObject.SetActive(false);
- SetServerInfo("Offline", "None");
- isMatchmaking = false;
- }
- }
- public void DisplayIsConnecting()
- {
- var _this = this;
- infoPanel.Display("Connecting...", "Cancel", () => { _this.backDelegate(); });
- }
- public void SetServerInfo(string status, string host)
- {
- statusInfo.text = status;
- hostInfo.text = host;
- }
- public delegate void BackButtonDelegate();
- public BackButtonDelegate backDelegate;
- public void GoBackButton()
- {
- backDelegate();
- }
- // ----------------- Server management
- public void SimpleBackClbk()
- {
- ChangeTo(mainMenuPanel);
- }
- public void StopHostClbk()
- {
- if (isMatchmaking)
- {
- this.matchMaker.DestroyMatch((NetworkID)_currentMatchID, OnMatchDestroyed);
- _disconnectServer = true;
- }
- else
- {
- StopHost();
- }
- ChangeTo(mainMenuPanel);
- }
- public void StopClientClbk()
- {
- StopClient();
- if (isMatchmaking)
- {
- StopMatchMaker();
- }
- ChangeTo(mainMenuPanel);
- }
- public void StopServerClbk()
- {
- StopServer();
- ChangeTo(mainMenuPanel);
- }
- public void StopGameClbk()
- {
- SendReturnToLobby();
- ChangeTo(lobbyPanel);
- }
- //===================
- public override void OnStartHost()
- {
- base.OnStartHost();
- ChangeTo(lobbyPanel);
- backDelegate = StopHostClbk;
- SetServerInfo("Hosting", networkAddress);
- }
- public override void OnClientConnect(NetworkConnection conn)
- {
- Debug.Log("Client Connected!");
- base.OnClientConnect(conn);
- infoPanel.gameObject.SetActive(false);
- if (!NetworkServer.active)
- {//only to do on pure client (not self hosting client)
- ChangeTo(lobbyPanel);
- backDelegate = StopClientClbk;
- SetServerInfo("Client", networkAddress);
- }
- }
- public override void OnMatchCreate(UnityEngine.Networking.Match.CreateMatchResponse matchInfo)
- {
- base.OnMatchCreate(matchInfo);
- _currentMatchID = (System.UInt64)matchInfo.networkId;
- }
- public void OnMatchDestroyed(BasicResponse resp)
- {
- if (_disconnectServer)
- {
- StopMatchMaker();
- StopHost();
- }
- }
- // ----------------- Server callbacks ------------------
- //we want to disable the button JOIN if we don't have enough player
- //But OnLobbyClientConnect isn't called on hosting player. So we override the lobbyPlayer creation
- public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
- {
- Debug.Log("Create Lobby Player!");
- GameObject obj = Instantiate(lobbyPlayerPrefab.gameObject) as GameObject;
- LobbyPlayer newPlayer = obj.GetComponent<LobbyPlayer>();
- newPlayer.RpcToggleJoinButton(numPlayers + 1 >= minPlayer); ;
- for (int i = 0; i < numPlayers; ++i)
- {
- LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
- if (p != null)
- {
- p.RpcToggleJoinButton(numPlayers + 1 >= minPlayer);
- }
- }
- return obj;
- }
- public override void OnLobbyServerDisconnect(NetworkConnection conn)
- {
- for (int i = 0; i < numPlayers; ++i)
- {
- LobbyPlayer p = lobbySlots[i] as LobbyPlayer;
- if (p != null)
- {
- p.RpcToggleJoinButton(numPlayers >= minPlayer);
- }
- }
- }
- public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
- {
- //This hook allows you to apply state data from the lobby-player to the game-player
- //just subclass "LobbyHook" and add it to the lobby object.
- if (_lobbyHooks)
- _lobbyHooks.OnLobbyServerSceneLoadedForPlayer(this, lobbyPlayer, gamePlayer);
- return true;
- }
- // ----------------- Client callbacks ------------------
- public override void OnClientDisconnect(NetworkConnection conn)
- {
- base.OnClientDisconnect(conn);
- ChangeTo(mainMenuPanel);
- }
- public override void OnClientError(NetworkConnection conn, int errorCode)
- {
- ChangeTo(mainMenuPanel);
- infoPanel.Display("Cient error : " + (errorCode == 6 ? "timeout" : errorCode.ToString()), "Close", null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement