SHARE
TWEET

Untitled

a guest Jul 11th, 2015 310 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using BeardedManStudios.Network;
  5. using BeardedManStudios.Forge;
  6.  
  7. public class NetworkMgr : MonoBehaviour {
  8.  
  9.         [HideInInspector]
  10.         public static readonly string gameTypeName = "Neomex_Exowar_2_Neomex-Studio";
  11.  
  12.         public ushort port = 20555;
  13.  
  14.         void Awake()
  15.         {
  16.         }
  17.  
  18.         void Start()
  19.         {
  20.                 DontDestroyOnLoad (this);
  21.  
  22.                 Networking.InitializeFirewallCheck(port);
  23.  
  24.                 if (MultiplayerMenuUI.server) {
  25.                         StartServer ();
  26.                 }
  27.                 if (MultiplayerMenuUI.client) {
  28.                         if(MultiplayerJoinMenuUI.directJoin) {
  29.                                 StartDirectClient();
  30.                         } else {
  31.                                 StartClient ();
  32.                         }
  33.                 }
  34.         }
  35.  
  36.         void StartServer() {
  37.                 NetWorker socket = Networking.Host (port, Networking.TransportationProtocolType.UDP, 3, true);
  38.  
  39.                 Networking.Sockets[port].connected += OnServerConnected;
  40.                 Networking.Sockets[port].disconnected += OnServerDisconnected;
  41.                 Networking.Sockets[port].playerConnected += OnServerPlayerConnected;
  42.                 Networking.Sockets[port].playerDisconnected += OnServerPlayerDisconnected;
  43.                 //Networking.Sockets[port].error += OnServerError;
  44.  
  45.                 Networking.SetPrimarySocket(socket);
  46.         }
  47.  
  48.         void OnServerConnected() {
  49.                 Debug.Log ("Server connected!");
  50.  
  51.                 GetComponent<LobbyMenuUI> ().enabled = true;
  52.  
  53.         }
  54.  
  55.         void OnServerDisconnected() {
  56.                 Debug.Log ("Server disconnected!");
  57.         }
  58.  
  59.         void OnServerPlayerConnected(NetworkingPlayer player) {
  60.                 Debug.Log ("Player connected: " + player.Ip);
  61.         }
  62.  
  63.         void OnServerPlayerDisconnected(NetworkingPlayer player) {
  64.                 Debug.Log ("Player disconnected: " + player.Ip);
  65.         }
  66.  
  67.         void OnServerError(NetworkException exception/*System.Exception exception*/) {
  68.                 Debug.Log ("Server error: " + exception.Message);
  69.         }
  70.  
  71.         void StartClient() {
  72.         }
  73.  
  74.         void StartDirectClient() {
  75.                 NetWorker socket = Networking.Connect (MultiplayerJoinMenuUI.directIp, port, Networking.TransportationProtocolType.UDP, true);
  76.  
  77.                 Networking.Sockets[port].connected += OnClientConnected;
  78.                 Networking.Sockets[port].disconnected += OnClientDisconnected;
  79.                 Networking.Sockets[port].playerConnected += OnClientPlayerConnected;
  80.                 Networking.Sockets[port].playerDisconnected += OnClientPlayerDisconnected;
  81.                 //Networking.Sockets[port].error += OnClientError;
  82.                 Networking.Sockets[port].serverDisconnected += OnClientServerDisconnected;
  83.  
  84.                 Networking.SetPrimarySocket(socket);
  85.         }
  86.  
  87.         void OnClientConnected() {
  88.                 Debug.Log ("Client connected!");
  89.  
  90.                 GetComponent<LobbyMenuUI> ().enabled = true;
  91.         }
  92.        
  93.         void OnClientDisconnected() {
  94.                 Debug.Log ("Client disconnected!");
  95.         }
  96.        
  97.         void OnClientPlayerConnected(NetworkingPlayer player) {
  98.                 Debug.Log ("Player connected: " + player.Ip);
  99.         }
  100.        
  101.         void OnClientPlayerDisconnected(NetworkingPlayer player) {
  102.                 Debug.Log ("Player disconnected: " + player.Ip);
  103.         }
  104.        
  105.         void OnClientError(/*NetworkException exception*/ System.Exception exception) {
  106.                 Debug.Log ("Client error: " + exception.Message);
  107.         }
  108.  
  109.         void OnClientServerDisconnected(string message) {
  110.                 Debug.Log ("Disconnected: " + message);
  111.         }
  112.  
  113.         void Update()
  114.         {
  115.         }
  116. }
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