Advertisement
Boomsma95

Tutorial Script: Menumanager

Mar 26th, 2013
2,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.70 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MenuManager : MonoBehaviour
  5. {
  6.     public MenuManager instance;
  7.  
  8.     public string CurrentMenu;
  9.  
  10.     public string MatchName = "";
  11.     public string MatchPassword = "";
  12.     public int MatchMaxPlayers = 32;
  13.  
  14.     private Vector2 ScrollLobby = Vector2.zero;
  15.     private string iptemp = "127.0.0.1";
  16.  
  17.     void Start()
  18.     {
  19.         instance = this;
  20.         CurrentMenu = "Main";
  21.         MatchName = "HeyHeyWelcome " + Random.Range(0, 5000);
  22.     }
  23.  
  24.     void FixedUpdate()
  25.     {
  26.         instance = this;
  27.     }
  28.  
  29.     void OnGUI()
  30.     {
  31.         if (CurrentMenu == "Main")
  32.             Menu_Main();
  33.         if (CurrentMenu == "Lobby")
  34.             Menu_Lobby();
  35.         if (CurrentMenu == "Host")
  36.             Menu_HostGame();
  37.         if (CurrentMenu == "ChoMap")
  38.             Menu_ChooseMap();
  39.     }
  40.  
  41.     public void NavigateTo(string nextmenu)
  42.     {
  43.         CurrentMenu = nextmenu;
  44.     }
  45.  
  46.     private void Menu_Main()
  47.     {
  48.         if (GUI.Button(new Rect(10, 10, 200, 50), "Host Game"))
  49.         {
  50.             NavigateTo("Host");
  51.         }
  52.         if (GUI.Button(new Rect(10, 70, 200, 50), "Refresh"))
  53.         {
  54.             MasterServer.RequestHostList("DeathMatch");
  55.         }
  56.  
  57.         GUI.Label(new Rect(220, 10, 130, 30), "Player Name");
  58.         MultiplayerManager.instance.PlayerName = GUI.TextField(new Rect(350, 10, 150, 30), MultiplayerManager.instance.PlayerName);
  59.         if (GUI.Button(new Rect(510, 10, 100, 30), "Save Name"))
  60.         {
  61.             PlayerPrefs.SetString("PlayerName", MultiplayerManager.instance.PlayerName);
  62.         }
  63.  
  64.         GUI.Label(new Rect(220, 50, 130, 30), "Direct Connect");
  65.         iptemp = GUI.TextField(new Rect(350, 50, 150, 30), iptemp);
  66.         if (GUI.Button(new Rect(510, 50, 100, 30), "Connect"))
  67.         {
  68.             Network.Connect(iptemp, 2550);
  69.         }
  70.  
  71.         GUILayout.BeginArea(new Rect(Screen.width - 400, 0, 400, Screen.height), "Server List", "Box");
  72.         GUILayout.Space(20);
  73.         foreach (HostData match in MasterServer.PollHostList())
  74.         {
  75.             GUILayout.BeginHorizontal("Box");
  76.  
  77.             GUILayout.Label(match.gameName);
  78.             if (GUILayout.Button("Connect"))
  79.             {
  80.                 Network.Connect(match);
  81.             }
  82.  
  83.             GUILayout.EndHorizontal();
  84.         }
  85.  
  86.         GUILayout.EndArea();
  87.     }
  88.  
  89.     private void Menu_HostGame()
  90.     {
  91.         //Buttons Host Game
  92.         if (GUI.Button(new Rect(10, 10, 200, 50), "Back"))
  93.         {
  94.             NavigateTo("Main");
  95.         }
  96.  
  97.         if (GUI.Button(new Rect(10, 60, 200, 50), "Start Server"))
  98.         {
  99.             MultiplayerManager.instance.StartServer(MatchName, MatchPassword, MatchMaxPlayers);
  100.         }
  101.  
  102.         if (GUI.Button(new Rect(10, 160, 200, 50), "Choose Map"))
  103.         {
  104.             NavigateTo("ChoMap");
  105.         }
  106.  
  107.         GUI.Label(new Rect(220, 10, 130, 30), "Match Name");
  108.         MatchName = GUI.TextField(new Rect(400, 10, 200, 30), MatchName);
  109.  
  110.         GUI.Label(new Rect(220, 50, 130, 30), "Match Password");
  111.         MatchPassword = GUI.PasswordField(new Rect(400, 50, 200, 30), MatchPassword, '*');
  112.  
  113.         GUI.Label(new Rect(220, 90, 130, 30), "Match Max Players");
  114.         GUI.Label(new Rect(400, 90, 200, 30), (MatchMaxPlayers + 1).ToString());
  115.         MatchMaxPlayers = Mathf.Clamp(MatchMaxPlayers, 8, 31);
  116.  
  117.         if (GUI.Button(new Rect(425, 90, 30, 20), "+"))
  118.             MatchMaxPlayers += 2;
  119.         if (GUI.Button(new Rect(455, 90, 30, 20), "-"))
  120.             MatchMaxPlayers -= 2;
  121.  
  122.         GUI.Label(new Rect(650, 10, 130, 30), MultiplayerManager.instance.CurrentMap.MapName);
  123.     }
  124.  
  125.     private void Menu_Lobby()
  126.     {
  127.         ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth(200));
  128.  
  129.         foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList)
  130.         {
  131.             if (pl.PlayerNetwork == Network.player)
  132.                 GUI.color = Color.blue;
  133.             GUILayout.Box(pl.PlayerName);
  134.             GUI.color = Color.white;
  135.         }
  136.  
  137.         GUILayout.EndScrollView();
  138.  
  139.         GUI.Box(new Rect(250, 10, 200, 40), MultiplayerManager.instance.CurrentMap.MapName);
  140.  
  141.         if (Network.isServer)
  142.         {
  143.             if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 80, 200, 40), "Start Match"))
  144.             {
  145.                 MultiplayerManager.instance.networkView.RPC("Client_LoadMultiplayerMap", RPCMode.AllBuffered, MultiplayerManager.instance.CurrentMap.MapLoadName, MultiplayerManager.instance.oldprefix + 1);
  146.                 MultiplayerManager.instance.oldprefix += 1;
  147.                 MultiplayerManager.instance.IsMatchStarted = true;
  148.             }
  149.         }
  150.         if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 40, 200, 40), "Disconnect"))
  151.         {
  152.             Network.Disconnect();
  153.         }
  154.     }
  155.  
  156.     private void Menu_ChooseMap()
  157.     {
  158.         if (GUI.Button(new Rect(10, 10, 200, 50), "Back"))
  159.         {
  160.             NavigateTo("Host");
  161.         }
  162.  
  163.         GUI.Label(new Rect(220, 10, 130, 30), "Choose Map");
  164.         GUILayout.BeginArea(new Rect(350, 10, 150, Screen.height));
  165.  
  166.         foreach(MapSetting map in MultiplayerManager.instance.MapList)
  167.         {
  168.             if (GUILayout.Button(map.MapName))
  169.             {
  170.                 NavigateTo("Host");
  171.                 MultiplayerManager.instance.CurrentMap = map;
  172.             }
  173.         }
  174.  
  175.         GUILayout.EndArea();
  176.     }
  177.  
  178.     void OnServerInitialized()
  179.     {
  180.         NavigateTo("Lobby");
  181.     }
  182.  
  183.     void OnConnectedToServer()
  184.     {
  185.         NavigateTo("Lobby");
  186.     }
  187.  
  188.     void OnDisconnectedFromServer(NetworkDisconnection info)
  189.     {
  190.         NavigateTo("Main");
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement