Search Unity

*Help*Networking Menu Code for Coop games

Discussion in 'Multiplayer' started by YeOldeSnake, Sep 11, 2010.

  1. YeOldeSnake

    YeOldeSnake

    Joined:
    Aug 7, 2010
    Posts:
    29
    This is my networking code , everything regarding connecting to and starting servers is here.
    It seems to have some problem connecting to people , if someone could help me on this


    Code (csharp):
    1. var mousesensitivity :float= 10.0;
    2. //var mySkin : GUISkin;
    3. var hostServerPort:String;
    4. var joinServerIP:String;
    5. var playerName:String;
    6. var isinoptions = 0;
    7. var infocastTextShow;
    8. var connectStatus:String;
    9. var level:String="Level 2";
    10. var levelPrefix:int=2;
    11. var netwPlayer1Name:String;
    12. var netwPlayer2Name:String="Not Connected";
    13. function Awake(){
    14.  
    15.  
    16. if (PlayerPrefs.HasKey("Player Name")){
    17. playerName=PlayerPrefs.GetString("Player Name");
    18. isinoptions=0;
    19. }
    20. else if(PlayerPrefs.HasKey("Player Name")==false)
    21. isinoptions=10;
    22. }
    23.  
    24.  
    25.  
    26. function OnGUI () {
    27. //GUI.skin=mySkin;
    28.  
    29. if(isinoptions==10 ){
    30. GUI.Box (Rect (13,10,240,120), "Input your name");
    31. GUI.Label(Rect(30,80,300,120),"Name shouldnt be over 40 characters");
    32. playerName =GUI.TextField (Rect (30, 60, 150, 20),playerName);
    33. GUI.Label(Rect(30,100,300,120),"Name Length:"+playerName.length);
    34. if (GUI.Button (Rect (25,160,50,20), "OK")) {
    35. if(playerName.length>40)
    36. playerName="I abuse big names";
    37. isinoptions=0;
    38.  
    39. PlayerPrefs.SetString("Player Name" ,playerName);
    40. }
    41.  
    42. }
    43.  
    44.  
    45. if(isinoptions==0 ){
    46.  
    47.  
    48.  
    49.  
    50. //MAIN MENU
    51. GUI.Box (Rect (100,10,120,190), "Main Menu");
    52.  
    53.    if (GUI.Button (Rect (105,40,110,20), "New Game")) {
    54.         isinoptions=3;
    55.     }
    56.  
    57.     if (GUI.Button (Rect (105,70,110,20), "Online Coop")) {
    58. isinoptions=2;
    59. }
    60.  
    61. if (GUI.Button (Rect (105,100,110,20), "Options")) {
    62. isinoptions=1;
    63. }
    64.  
    65. if (GUI.Button (Rect (105,130,110,20), "Change Name")) {
    66. isinoptions=10;
    67. }
    68.  
    69.  
    70.  if(GUI.Button (Rect (105,160,110,20), "Quit")) {
    71.         Application.Quit();
    72.     }
    73.  
    74. }
    75.  
    76. if(isinoptions == 1){
    77. //OPTIONS MENU
    78. GUI.Box (Rect (13,10,180,120), "Options");
    79.  
    80.  
    81.    
    82.  
    83. if (GUI.Button (Rect (25,80,150,30), "Decrease Quality Level")) {
    84.         QualitySettings.DecreaseLevel();
    85.     }
    86.     if (GUI.Button (Rect (25,40,150,30), "Increase Quality Level")) {
    87.         QualitySettings.IncreaseLevel();
    88.     }
    89.  
    90. if (GUI.Button (Rect (25,160,50,20), "Back")) {
    91. isinoptions=0;
    92. }
    93.  
    94. }
    95.  
    96. if(isinoptions ==3){
    97. //OPTIONS MENU
    98. GUI.Box (Rect (13,10,180,120), "New Game");
    99.  
    100.  
    101.    
    102.  
    103. if (GUI.Button (Rect (25,80,150,30), "Play the tutorial")) {
    104.        Application.LoadLevel("LvL 1");
    105.     }
    106.     if (GUI.Button (Rect (25,40,150,30), "Skip the tutorial")) {
    107.        Application.LoadLevel("Level 2");
    108.     }
    109.  
    110. if (GUI.Button (Rect (25,160,50,20), "Back")) {
    111. isinoptions=0;
    112. }
    113.  
    114. }
    115.  
    116. if(isinoptions == 2){
    117. //big deeeal
    118. GUI.Box (Rect (13,10,180,120), "Online Coop Menu");
    119.  
    120.  
    121.    
    122.  
    123. if (GUI.Button (Rect (25,40,150,30), "Host Server")) {
    124.         isinoptions=6;
    125.         Network.useNat=true;
    126.         Network.InitializeServer(2,27015);
    127.        
    128.     }
    129.     if (GUI.Button (Rect (25,80,150,30), "Join Server")) {
    130.         isinoptions = 7;
    131.     }
    132.  
    133. if (GUI.Button (Rect (25,160,50,20), "Back")) {
    134. isinoptions=0;
    135. }
    136.  
    137. }
    138. if(isinoptions == 5){
    139. GUI.Box (Rect (13,10,600,120), "Lobby");
    140. GUI.Label(Rect (20,30,300,120),"Player 2: "+ netwPlayer1Name+ "   (host)");
    141. GUI.Label(Rect (20,50,300,120),"Player 1: "+ playerName);
    142. GUI.Label(Rect (20,70,300,120),"IP Address: "+Network.natFacilitatorIP);
    143. GUI.Label(Rect (20,90,300,120),"Map to Load: "+ level);
    144. if (Network.peerType == NetworkPeerType.Disconnected){
    145. isinoptions=7;
    146. connectStatus="Disconnected";
    147. }
    148. if (GUI.Button (Rect (25,160,100,20), "Disconnect")) {
    149. networkView.RPC("PlayerTwoNameSending", RPCMode.All,"  ");
    150. Network.Disconnect();
    151. connectStatus=" ";
    152. isinoptions=0;
    153. }
    154.  
    155. }
    156.  
    157. if(isinoptions == 6){
    158. GUI.Box (Rect (13,10,300,120), "Online Coop Menu");
    159. GUI.Label(Rect (20,30,300,120),"Player 1: "+ netwPlayer1Name);
    160. GUI.Label(Rect (20,50,300,120),"Player 2: "+ netwPlayer2Name);
    161. GUI.Label(Rect (20,70,300,120),"Map to Load: "+ level);
    162. GUI.Label(Rect (20,90,300,120),"IP Address: "+Network.natFacilitatorIP);
    163.  
    164. GUI.Box (Rect (320,10,180,120), "Maps");
    165. if (GUI.Button (Rect (335,50,100,20), "Example Level")) {
    166. networkView.RPC("SendLevelName", RPCMode.OthersBuffered,"Example Level");
    167. levelPrefix=1;
    168. level="Example Level";
    169.  
    170. }
    171. if (GUI.Button (Rect (335,70,100,20), "Level 1")) {
    172. levelPrefix=2;
    173. level="Level 1 Networked";
    174. networkView.RPC("SendLevelName", RPCMode.OthersBuffered,"Level 1");
    175. }
    176. if (GUI.Button (Rect (30,180,140,20), "Start")) {
    177.         LoadLevelOf();
    178. }
    179.  
    180.  
    181. if (GUI.Button (Rect (30,200,140,20), "Shut Down Server")) {
    182. Network.Disconnect();
    183. connectStatus=" ";
    184. isinoptions=0;
    185. }
    186.  
    187. }
    188.  
    189.  
    190. if(isinoptions == 7){
    191. GUI.Label(Rect(25,130,300,300),connectStatus);
    192. GUI.Box (Rect (13,10,180,120), "Join Server");
    193. joinServerIP = GUI.TextField (Rect (30, 30, 100, 20), joinServerIP);
    194. GUI.Label(Rect(30,50,150,120),"Type the ip of the server you want to connect here");
    195. if (GUI.Button (Rect (25,100,100,20), "Connect")) {
    196. connectStatus="Connecting";
    197. Network.useNat=false;
    198. Network.Connect(joinServerIP,27015);
    199. Network.useNat=true;
    200.  
    201. }
    202.  
    203. //parseInt (myString)
    204. if (GUI.Button (Rect (25,160,50,20), "Back")) {
    205. connectStatus="  ";
    206. isinoptions=0;
    207. }
    208. }
    209.  
    210.  
    211.  
    212. }
    213.  
    214.  
    215.  
    216.  
    217.  
    218. function Update (){
    219.  
    220.    
    221.    
    222.     if (Input.GetKeyDown(KeyCode.P))
    223.         Application.CaptureScreenshot("Screenshot.png");
    224.      //if(PlayerPrefs.GetInt("funkach")==1337)
    225.        //  funkTextTimer=0;
    226.      }
    227.        
    228.      function OnFailedToConnect(error: NetworkConnectionError)
    229. {
    230. connectStatus="Could not connect to server:  "+ error;
    231. Debug.Log("Could not connect to server: "+ error);
    232.  
    233. }  
    234.        
    235.        
    236.       function OnConnectedToServer(){
    237.  connectStatus="Connected , Wait for the host to select a map and start the game";   
    238.     networkView.RPC("PlayerTwoNameSending", RPCMode.AllBuffered,playerName);  
    239. isinoptions=5;
    240.  
    241. }
    242.  
    243.  
    244. @RPC
    245.  
    246. function LoadLevelOf(){
    247.  
    248.         if (Network.isServer){
    249.  
    250.                 networkView.RPC("LoadLevel", RPCMode.All, level,levelPrefix);
    251.         }
    252.  
    253. }
    254.  
    255.  
    256.  
    257. @RPC
    258. function LoadLevel (levelInt :String, levelPrefix :int)
    259. {
    260.     lastLevelPrefix = levelPrefix;
    261.  
    262.         // There is no reason to send any more data over the network on the default channel,
    263.         // because we are about to load the level, thus all those objects will get deleted anyway
    264.         //DebugConsole.LogMessage("loading a new level, sending disabled");
    265.         Network.SetSendingEnabled(0, false);   
    266.  
    267.         // We need to stop receiving because first the level must be loaded first.
    268.         // Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
    269.         Network.isMessageQueueRunning = false;
    270.  
    271.         // All network views loaded from a level will get a prefix into their NetworkViewID.
    272.         // This will prevent old updates from clients leaking into a newly created scene.
    273.         Network.SetLevelPrefix(levelPrefix);
    274.         Application.LoadLevel(levelInt);
    275.         //yield;
    276.         //yield;
    277.  
    278.         // Allow receiving data again
    279.         //Network.isMessageQueueRunning = true;
    280.         // Now the level has been loaded and we can start sending out data to clients
    281.         //Network.SetSendingEnabled(0, true);
    282.         //Debug.Log("Sending enabled again");
    283.  
    284.  
    285.         //for (var go in FindObjectsOfType(GameObject))
    286.             //go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);  
    287. }
    288.  
    289. function OnLevelWasLoaded () {
    290.     // Allow receiving data again
    291.     Network.isMessageQueueRunning = true;
    292.     // Now the level has been loaded and we can start sending out data to clients
    293.     Network.SetSendingEnabled(0, true);
    294.     //DebugConsole.LogMessage("Level finished loading. Sending enabled again");
    295.  
    296.  
    297.     //for (var go in FindObjectsOfType(GameObject))
    298.         //go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);  
    299. }
    300. @RPC
    301. function PlayerOneNameSending(name1:String){
    302. netwPlayer1Name=name1;
    303. //netwPlayer2Name=name2;
    304. }
    305. @RPC
    306. function PlayerTwoNameSending(name2:String){
    307. //netwPlayer1Name=name1;
    308. netwPlayer2Name=name2;
    309. }
    310.  
    311. function OnServerInitialized(){
    312. Network.useNat = !Network.HavePublicAddress();
    313. networkView.RPC("PlayerOneNameSending", RPCMode.AllBuffered, playerName);
    314. }
    315.  
    316. @RPC
    317. function SendLevelName(levelName:String){
    318. level=levelName;
    319. }
    320.  
    321.