Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Created by CDTDev
- File: MultiplayerManager.c#
- */
- using UnityEngine;
- using System.Collections;
- public class MultiplayerManager : MonoBehaviour {
- private const string typeName = "RusHAlpha";
- public string ServerName = "Server01";
- private HostData[] hostList;
- public Transform PlayerPref,SP,Cam;
- public string nameofplayer,message="",ip="127.0.0.1",port="25000";
- public string[] ChatMessages = new string[]{"System: Welcome on this Server"};
- void Start(){
- RefreshHostList ();
- }
- void OnGUI()
- {
- if(!Network.isClient&&!Network.isServer)
- {
- if(GUI.Button(new Rect(10,10,100,20),"Start Server"))
- StartServer();
- if(GUI.Button(new Rect(10,30,100,20),"Direct Connect"))
- ConnectServer();
- if(GUI.Button(new Rect(10,50,100,20),"Refresh"))
- RefreshHostList();
- if (hostList != null)
- {
- for (int i = 0; i < hostList.Length; i++)
- {
- GUI.Box(new Rect(390,90 + (110 * i),320,120),"");
- if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), "Connect to Server: " + hostList[i].gameName))
- JoinServer(hostList[i]);
- }
- }
- //TexFields
- GUI.Label(new Rect(110,10,100,50),"Server Name");
- ServerName = GUI.TextField(new Rect(200,10,100,20),ServerName);
- GUI.Label(new Rect(310,10,100,50),"Server Port");
- port = GUI.TextField(new Rect(400,10,50,20),port);
- GUI.Label(new Rect(110,30,100,50),"Player Name");
- nameofplayer = GUI.TextField(new Rect(200,30,100,20),nameofplayer);
- GUI.Label(new Rect(110,50,100,50),"Direct IP");
- ip = GUI.TextField(new Rect(200,50,100,20),ip);
- //EndTexFields
- }else{
- GUI.Box(new Rect(Screen.width -260,10,250,100),"Chat");
- for(int i = 0; i < ChatMessages.Length;i++)
- {
- GUI.Label(new Rect(Screen.width - 250,20 + (10 * i),200,50),ChatMessages[i]);
- }
- message = GUI.TextField(new Rect(Screen.width -260,110,160,20),message);
- if(GUI.Button(new Rect(Screen.width -90,110,80,20),"Send")){
- networkView.RPC("SendMessage",RPCMode.All,nameofplayer + ": " + message);
- message = "";
- }
- }
- }
- private void JoinServer(HostData hostData)
- {
- Network.Connect(hostData);
- }
- private void RefreshHostList()
- {
- MasterServer.RequestHostList(typeName);
- }
- void OnMasterServerEvent(MasterServerEvent msEvent)
- {
- if (msEvent == MasterServerEvent.HostListReceived)
- hostList = MasterServer.PollHostList();
- }
- void StartServer()
- {
- bool useNat = !Network.HavePublicAddress ();
- Network.InitializeServer (32, int.Parse(port), useNat);
- MasterServer.RegisterHost(typeName, ServerName);
- }
- void ConnectServer()
- {
- Network.Connect (ip,25000);
- }
- void OnServerInitialized() {
- Network.Instantiate (PlayerPref, SP.transform.position, transform.rotation,0);
- Cam.gameObject.SetActive (false);
- }
- void OnConnectedToServer() {
- Network.Instantiate (PlayerPref, SP.transform.position, transform.rotation,0);
- Cam.gameObject.SetActive (false);
- }
- void OnFailedToConnect(NetworkConnectionError error) {
- Debug.Log("Could not connect to server: " + error);
- }
- [RPC]
- void SendMessage(string mes)
- {
- int i = 0;
- if(i < ChatMessages.Length)
- {
- i++;
- ChatMessages = new string[]{ChatMessages[i],mes};
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement