SHARE
TWEET

Untitled

a guest Jul 25th, 2014 3,424 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [System.Serializable]
  5. public class Map
  6. {
  7.     public string name;
  8.     public string levelName;
  9. }
  10.  
  11. public class MainMenu : MonoBehaviour
  12. {
  13.     public int curMenu = 0;
  14.  
  15.     private string[] settings;
  16.     private int curIndex;
  17.  
  18.     public float dif = 1;
  19.     HostData[] datas;
  20.     public Vector2 scroll;
  21.     private string gameName = "Server 666";
  22.  
  23.     //You have to change this!
  24.     public string uniqueGameName = "MyUniqueGameName";
  25.  
  26.     //After which time will the server list be refreshed ?
  27.     public float refreshTime = 10;
  28.  
  29.     public Map[] maps;
  30.     public int curMapIndex;
  31.  
  32.     void Start()
  33.     {
  34.         InvokeRepeating("GetHostList", 0, refreshTime);
  35.         settings = QualitySettings.names;
  36.         Network.isMessageQueueRunning = false;
  37.     }
  38.  
  39.     void GetHostList()
  40.     {
  41.         MasterServer.RequestHostList(uniqueGameName);
  42.     }
  43.  
  44.     void Update()
  45.     {
  46.         dif = (Screen.width / 12.8f) / 100;
  47.         datas = MasterServer.PollHostList();
  48.     }
  49.  
  50.     void OnGUI()
  51.     {
  52.         if (curMenu == 0)
  53.         {
  54.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 80 * dif, 200 * dif, 50 * dif), "Play"))
  55.             {
  56.                 curMenu = 1;
  57.             }
  58.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 25 * dif, 200 * dif, 50 * dif), "Options"))
  59.             {
  60.                 curMenu = 2;
  61.             }
  62.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 + 30 * dif, 200 * dif, 50 * dif), "Quit"))
  63.             {
  64.                 Application.Quit();
  65.             }
  66.  
  67.         }
  68.         else if (curMenu == 1)
  69.         {
  70.             GUI.Box(new Rect(20 * dif, 20 * dif, 200 * dif, 200 * dif), "");
  71.             GUILayout.BeginArea(new Rect(25 * dif, 25 * dif, 190 * dif, 190 * dif));
  72.             gameName = GUILayout.TextField(gameName);
  73.             if (GUILayout.Button("Start Server"))
  74.             {
  75.                 Network.InitializeSecurity();
  76.                 Network.InitializeServer(17, 25001, /*!Network.HavePublicAddress()*/ true);
  77.                 MasterServer.RegisterHost(uniqueGameName, gameName);
  78.             }
  79.             if (GUILayout.Button("Select Map"))
  80.             {
  81.                 curMenu = 5;
  82.             }
  83.             if (GUILayout.Button("Direct Connect"))
  84.             {
  85.                 Network.Connect("127.0.0.1", 25001);
  86.             }
  87.             if (GUILayout.Button("Back"))
  88.             {
  89.                 curMenu = 0;
  90.             }
  91.             GUILayout.EndArea();
  92.             GUI.Box(new Rect(Screen.width - 400 * dif, 0, 400 * dif, Screen.height), "");
  93.             GUILayout.BeginArea(new Rect(Screen.width - 400 * dif, 0, 400 * dif, Screen.height));
  94.             GUILayout.Label("Avaiable Servers: " + datas.Length);
  95.             scroll = GUILayout.BeginScrollView(scroll);
  96.             foreach (HostData data in datas)
  97.             {
  98.                 GUILayout.BeginHorizontal();
  99.                 GUILayout.Label(data.gameName + " Players: " + data.connectedPlayers + " / " + data.playerLimit);
  100.                 if (GUILayout.Button("Connect"))
  101.                 {
  102.                     Network.Connect(data);
  103.                 }
  104.                 GUILayout.EndHorizontal();
  105.             }
  106.             GUILayout.EndScrollView();
  107.             GUILayout.EndArea();
  108.         }
  109.         else if (curMenu == 2)
  110.         {
  111.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 80 * dif, 200 * dif, 50 * dif), "Audio"))
  112.             {
  113.                 curMenu = 3;
  114.             }
  115.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 25 * dif, 200 * dif, 50 * dif), "Graphics"))
  116.             {
  117.                 curMenu = 4;
  118.             }
  119.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 + 30 * dif, 200 * dif, 50 * dif), "Back"))
  120.             {
  121.                 curMenu = 0;
  122.             }
  123.         }
  124.         else if (curMenu == 3)
  125.         {
  126.             AudioListener.volume = GUI.HorizontalSlider(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 25 * dif, 200 * dif, 50 * dif), AudioListener.volume, 0f, 1f);
  127.             GUI.Label(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 45 * dif, 200 * dif, 50 * dif), "Volume: " + (AudioListener.volume * 100).ToString("F0") + " %");
  128.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 + 30 * dif, 200 * dif, 50 * dif), "Back"))
  129.             {
  130.                 curMenu = 2;
  131.             }
  132.         }
  133.         else if (curMenu == 4)
  134.         {
  135.             if (GUI.Button(new Rect(Screen.width / 2 - 75 * dif, Screen.height / 2 - 25 * dif, 150 * dif, 50 * dif), settings[curIndex]))
  136.             {
  137.                 QualitySettings.SetQualityLevel(curIndex);
  138.             }
  139.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 25 * dif, 20 * dif, 50 * dif), "←"))
  140.             {
  141.                 if (curIndex != 0) curIndex--;
  142.                 else curIndex = settings.Length - 1;
  143.             }
  144.  
  145.             if (GUI.Button(new Rect(Screen.width / 2 + 80 * dif, Screen.height / 2 - 25 * dif, 20 * dif, 50 * dif), "→"))
  146.             {
  147.                 if (curIndex != settings.Length - 1) curIndex++;
  148.                 else curIndex = 0;
  149.             }
  150.  
  151.             //I only made switching between quality settings. You can make it much more complex if you want.
  152.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 + 30 * dif, 200 * dif, 50 * dif), "Back"))
  153.             {
  154.                 curMenu = 2;
  155.             }
  156.         }
  157.         else if (curMenu == 5)
  158.         {
  159.             if (GUI.Button(new Rect(Screen.width / 2 - 75 * dif, Screen.height / 2 - 25 * dif, 150 * dif, 50 * dif), maps[curMapIndex].name))
  160.             {
  161.             }
  162.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 - 25 * dif, 20 * dif, 50 * dif), "←"))
  163.             {
  164.                 if (curMapIndex != 0) curMapIndex--;
  165.                 else curMapIndex = maps.Length - 1;
  166.             }
  167.  
  168.             if (GUI.Button(new Rect(Screen.width / 2 + 80 * dif, Screen.height / 2 - 25 * dif, 20 * dif, 50 * dif), "→"))
  169.             {
  170.                 if (curMapIndex != maps.Length - 1) curMapIndex++;
  171.                 else curMapIndex = 0;
  172.             }
  173.  
  174.             if (GUI.Button(new Rect(Screen.width / 2 - 100 * dif, Screen.height / 2 + 30 * dif, 200 * dif, 50 * dif), "Back"))
  175.             {
  176.                 curMenu = 1;
  177.             }
  178.         }
  179.     }
  180.  
  181.     void OnServerInitialized()
  182.     {
  183.         networkView.RPC("LoadLevel", RPCMode.AllBuffered, maps[curMapIndex].levelName);
  184.         Debug.Log("Server initialized and ready");
  185.     }
  186.  
  187.     [RPC]
  188.     IEnumerator LoadLevel(string level)
  189.     {
  190.         Network.isMessageQueueRunning = false;
  191.         yield return new WaitForSeconds(1);
  192.         Application.LoadLevel(level);
  193.         Network.isMessageQueueRunning = true;
  194.     }
  195.  
  196.     void OnConnectedToServer()
  197.     {
  198.         Debug.Log("Connected to server");
  199.         //Network.isMessageQueueRunning = true;
  200.     }
  201. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top