Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. #pragma strict
  2. #pragma implicit
  3. #pragma downcast
  4.  
  5. private var windowRect1;
  6. private var windowRect2;
  7. private var windowRect3;
  8.  
  9. static var playNowMode : boolean = false;
  10. static var advancedMode : boolean = false;
  11. static var playNowModeStarted : float = 0.0;
  12.  
  13. static var myPlayerName : String = "MyPlayerName";
  14.  
  15. //GUI vars
  16. private var hostPlayers : int = 8;
  17. private var hostPort : int = 22222;
  18. private var connectPort : int;
  19. private var connectIP : String = "";
  20.  
  21. private var multiplayerScript : Connect;
  22. private var currentMenu : String = "";
  23.  
  24.  
  25. function Awake ()
  26. {
  27. Screen.lockCursor=false;
  28.  
  29. myPlayerName = PlayerPrefs.GetString("playerName");
  30.  
  31. multiplayerScript = GetComponent(Connect);
  32.  
  33. connectPort = hostPort = multiplayerScript.address.ipPort;
  34. connectIP = "127.0.0.1";
  35.  
  36. windowRect1 = Rect (Screen.width/2-310,Screen.height/2-90,380,280);
  37. windowRect2 = Rect (Screen.width/2+85,Screen.height/2-90,220,100);
  38. windowRect3 = Rect (Screen.width/2+85,Screen.height/2+55,220,100);
  39.  
  40. playNowMode=false;
  41. advancedMode=false;
  42. }
  43.  
  44.  
  45.  
  46.  
  47. function OnGUI ()
  48. {
  49. //If we've connected; load the game when it's ready to load
  50. if(Network.isClient || Network.isServer){
  51. //Since we're connected, load the game
  52. GUI.Box(Rect(Screen.width/4+0,Screen.height/2-30,450,50), "");
  53. if(Application.CanStreamedLevelBeLoaded ((Application.loadedLevel+1))){
  54. GUI.Label(Rect(Screen.width/4+10,Screen.height/2-25,285,150), "Starting the game!");
  55. Application.LoadLevel((Application.loadedLevel+1));
  56. }else{
  57. GUI.Label(Rect(Screen.width/4+10,Screen.height/2-25,285,150), "Loading the game: "+Mathf.Floor(Application.GetStreamProgressForLevel((Application.loadedLevel+1))*100)+" %");
  58. }
  59. return;
  60. }
  61.  
  62.  
  63. //Dirty error message popup
  64. if(multiplayerScript.errorMessage && multiplayerScript.errorMessage!=""){
  65. GUI.Box(Rect(Screen.width/2-100,Screen.height/2-160,200,60), "Error");
  66. GUI.Label(Rect(Screen.width/2-90,Screen.height/2-145,180,50), multiplayerScript.errorMessage);
  67. if(GUI.Button(Rect(Screen.width/2+40,Screen.height/2-110,50,20), "Close")){
  68. multiplayerScript.errorMessage="";
  69. }
  70. }
  71.  
  72.  
  73. if(advancedMode){
  74. if(GUI.Button(Rect(455,90,140,30), "Back to main menu")){
  75. currentMenu="";
  76. advancedMode=false;
  77. }
  78. windowRect1 = GUILayout.Window (0, windowRect1, listGUI, "Server Browser");
  79. windowRect3 = GUILayout.Window (2, windowRect3, hostGUI, "Create Game");
  80.  
  81. }else{
  82. GUI.Box (Rect (90, 180, 260, 105), "Playername");
  83. GUI.Label (Rect (100, 195, 250, 100), "Please enter your name:");
  84.  
  85. myPlayerName = GUI.TextField (Rect (178, 215, 147, 27), myPlayerName);
  86. if(GUI.changed){
  87. //Save the name changes
  88. PlayerPrefs.SetString("playerName", myPlayerName);
  89. }
  90.  
  91. if(myPlayerName==""){
  92. GUI.Label (Rect (100, 240, 260, 100), "After entering your name you can start playing!");
  93. return;
  94. }
  95.  
  96. GUI.Label (Rect (100, 240, 260, 100), "Click on quickplay to start playing right away!");
  97.  
  98. if(GUI.Button(Rect(400,200,150,30), "Server Browser") ){
  99. currentMenu="advanced";
  100. advancedMode=true;
  101. }
  102.  
  103. }
  104. }
  105.  
  106. function hostGUI(id : int){
  107.  
  108. GUILayout.BeginVertical();
  109. GUILayout.Space(10);
  110. GUILayout.EndVertical();
  111.  
  112. GUILayout.BeginHorizontal();
  113. GUILayout.Space(10);
  114. GUILayout.Label("Use port: ");
  115. hostPort = parseInt(GUILayout.TextField(hostPort.ToString(), GUILayout.MaxWidth(75)));
  116. GUILayout.Space(10);
  117. GUILayout.EndHorizontal();
  118.  
  119. GUILayout.BeginHorizontal();
  120. GUILayout.Space(10);
  121. GUILayout.Label("Players: ");
  122. hostPlayers = parseInt(GUILayout.TextField(hostPlayers.ToString(), GUILayout.MaxWidth(75)));
  123. GUILayout.Space(10);
  124. GUILayout.EndHorizontal();
  125.  
  126.  
  127. GUILayout.BeginHorizontal();
  128. GUILayout.FlexibleSpace();
  129. // Start a new server
  130. if (GUILayout.Button ("Start hosting a server")){
  131. multiplayerScript.HostGame(hostPlayers, hostPort);
  132. }
  133. GUILayout.FlexibleSpace();
  134. GUILayout.EndHorizontal();
  135. }
  136.  
  137. private var scrollPosition : Vector2;
  138.  
  139. function listGUI (id : int) {
  140.  
  141. GUILayout.BeginVertical();
  142. GUILayout.Space(6);
  143. GUILayout.EndVertical();
  144.  
  145.  
  146. GUILayout.BeginHorizontal();
  147. GUILayout.Space(200);
  148.  
  149. // Refresh hosts
  150. if (GUILayout.Button ("Refresh available Servers"))
  151. {
  152. multiplayerScript.FindHosts();
  153. }
  154. multiplayerScript.FindHosts();
  155.  
  156. GUILayout.FlexibleSpace();
  157. GUILayout.EndHorizontal();
  158.  
  159. scrollPosition = GUILayout.BeginScrollView (scrollPosition);
  160.  
  161. var aHost : int = 0;
  162.  
  163. if(multiplayerScript.sortedList && multiplayerScript.sortedList.length>=1){
  164. for (var myElement in multiplayerScript.sortedList)
  165. {
  166. var element = multiplayerScript.hostData[myElement];
  167. GUILayout.BeginHorizontal();
  168.  
  169. // Do not display NAT enabled games if we cannot do NAT punchthrough
  170. if ( !(multiplayerScript.filterNATHosts && element.useNat) )
  171. {
  172. aHost=1;
  173. var name = element.gameName + " ";
  174. GUILayout.Label(name);
  175. GUILayout.FlexibleSpace();
  176. GUILayout.Label(element.connectedPlayers + "/" + element.playerLimit);
  177.  
  178. if(element.useNat){
  179. GUILayout.Label(".");
  180. }
  181. GUILayout.FlexibleSpace();
  182. GUILayout.Label("[" + element.ip[0] + ":" + element.port + "]");
  183. GUILayout.FlexibleSpace();
  184. if(!multiplayerScript.nowConnecting){
  185. if (GUILayout.Button("Connect")) {
  186. multiplayerScript.Connect(element.ip[0], element.port);
  187. }
  188. }else{
  189. GUILayout.Button("Connecting");
  190. }
  191. GUILayout.Space(15);
  192. }
  193. GUILayout.EndHorizontal();
  194. }
  195. }
  196. GUILayout.EndScrollView ();
  197. if(aHost==0){
  198. GUILayout.Label("No games hosted at the moment..");
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement