Advertisement
Guest User

MultiplayerManger.C# CDTDev

a guest
Jan 31st, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. /*
  2.     Created by CDTDev
  3.     File: MultiplayerManager.c#
  4. */
  5.  
  6. using UnityEngine;
  7. using System.Collections;
  8.  
  9. public class MultiplayerManager : MonoBehaviour {
  10.  
  11.     private const string typeName = "RusHAlpha";
  12.     public string ServerName = "Server01";
  13.    
  14.     private HostData[] hostList;
  15.  
  16.     public Transform PlayerPref,SP,Cam;
  17.  
  18.     public string nameofplayer,message="",ip="127.0.0.1",port="25000";
  19.  
  20.     public string[] ChatMessages = new string[]{"System: Welcome on this Server"};
  21.  
  22.     void Start(){
  23.         RefreshHostList ();
  24.     }
  25.  
  26.     void OnGUI()
  27.     {
  28.         if(!Network.isClient&&!Network.isServer)
  29.         {
  30.             if(GUI.Button(new Rect(10,10,100,20),"Start Server"))
  31.                 StartServer();
  32.  
  33.             if(GUI.Button(new Rect(10,30,100,20),"Direct Connect"))
  34.                 ConnectServer();
  35.  
  36.             if(GUI.Button(new Rect(10,50,100,20),"Refresh"))
  37.                 RefreshHostList();
  38.  
  39.  
  40.             if (hostList != null)
  41.             {
  42.                 for (int i = 0; i < hostList.Length; i++)
  43.                 {
  44.                     GUI.Box(new Rect(390,90 + (110 * i),320,120),"");
  45.                     if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), "Connect to Server: " + hostList[i].gameName))
  46.                         JoinServer(hostList[i]);
  47.                 }
  48.             }
  49.  
  50.             //TexFields
  51.             GUI.Label(new Rect(110,10,100,50),"Server Name");
  52.             ServerName = GUI.TextField(new Rect(200,10,100,20),ServerName);
  53.  
  54.             GUI.Label(new Rect(310,10,100,50),"Server Port");
  55.             port = GUI.TextField(new Rect(400,10,50,20),port);
  56.  
  57.             GUI.Label(new Rect(110,30,100,50),"Player Name");
  58.             nameofplayer = GUI.TextField(new Rect(200,30,100,20),nameofplayer);
  59.  
  60.             GUI.Label(new Rect(110,50,100,50),"Direct IP");
  61.             ip = GUI.TextField(new Rect(200,50,100,20),ip);
  62.             //EndTexFields
  63.  
  64.         }else{
  65.             GUI.Box(new Rect(Screen.width -260,10,250,100),"Chat");
  66.  
  67.             for(int i = 0; i < ChatMessages.Length;i++)
  68.             {
  69.                 GUI.Label(new Rect(Screen.width - 250,20 + (10 * i),200,50),ChatMessages[i]);
  70.             }
  71.             message = GUI.TextField(new Rect(Screen.width -260,110,160,20),message);
  72.  
  73.             if(GUI.Button(new Rect(Screen.width -90,110,80,20),"Send")){
  74.                 networkView.RPC("SendMessage",RPCMode.All,nameofplayer + ": " + message);
  75.                
  76.                 message = "";
  77.             }
  78.        
  79.         }
  80.     }
  81.  
  82.     private void JoinServer(HostData hostData)
  83.     {
  84.         Network.Connect(hostData);
  85.     }
  86.  
  87.     private void RefreshHostList()
  88.     {
  89.         MasterServer.RequestHostList(typeName);
  90.     }
  91.    
  92.     void OnMasterServerEvent(MasterServerEvent msEvent)
  93.     {
  94.         if (msEvent == MasterServerEvent.HostListReceived)
  95.             hostList = MasterServer.PollHostList();
  96.     }
  97.  
  98.     void StartServer()
  99.     {
  100.         bool useNat = !Network.HavePublicAddress ();
  101.         Network.InitializeServer (32, int.Parse(port), useNat);
  102.         MasterServer.RegisterHost(typeName, ServerName);
  103.     }
  104.  
  105.     void ConnectServer()
  106.     {
  107.         Network.Connect (ip,25000);
  108.     }
  109.  
  110.     void OnServerInitialized() {
  111.         Network.Instantiate (PlayerPref, SP.transform.position, transform.rotation,0);
  112.         Cam.gameObject.SetActive (false);
  113.     }
  114.  
  115.     void OnConnectedToServer() {
  116.         Network.Instantiate (PlayerPref, SP.transform.position, transform.rotation,0);
  117.         Cam.gameObject.SetActive (false);
  118.     }
  119.  
  120.     void OnFailedToConnect(NetworkConnectionError error) {
  121.         Debug.Log("Could not connect to server: " + error);
  122.     }
  123.  
  124.     [RPC]
  125.     void SendMessage(string mes)
  126.     {
  127.         int i = 0;
  128.         if(i < ChatMessages.Length)
  129.         {
  130.             i++;
  131.             ChatMessages = new string[]{ChatMessages[i],mes};
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement