G2A Many GEOs
SHARE
TWEET

Untitled

a guest Jul 5th, 2015 9 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.  
  6. public class Startup : MonoBehaviour {
  7.  
  8.     public string host = "127.0.0.1";
  9.     public int port = 15937;
  10.     public Networking.TransportationProtocolType protocolType = Networking.TransportationProtocolType.UDP;
  11.  
  12.     private NetWorker socket = null;
  13.     public Chat chat;
  14.  
  15.         void Start () {
  16.         Networking.InitializeFirewallCheck((ushort)port);
  17.         }
  18.  
  19.     public void StartServer() {
  20.         socket = Networking.Host((ushort)port, protocolType, 10);
  21.         socket.playerConnected += PlayerConnected;
  22.         socket.playerDisconnected += PlayerDisconnected;
  23.         chat.SendMessage("Starting server...");
  24.     }
  25.  
  26.     public void StartClient() {
  27.         socket = Networking.Connect("127.0.0.1", (ushort)port, protocolType);
  28.         chat.SendMessage("Starting client...");
  29.     }
  30.  
  31.     private void PlayerDisconnected(NetworkingPlayer player) {
  32.         chat.SendMessage(string.Format("Player [{0}] disconnected", player.Ip));
  33.     }
  34.  
  35.     private void PlayerConnected(NetworkingPlayer player) {
  36.         chat.SendMessage(string.Format("Player [{0}] connected", player.Ip));
  37.     }
  38. }
  39.  
  40.  
  41. //---- Chat.cs
  42. using UnityEngine;
  43. using System.Collections;
  44. using System.Collections.Generic;
  45. using UnityEngine.UI;
  46. using BeardedManStudios.Network;
  47.  
  48.  
  49. public class Chat : SimpleNetworkedMonoBehavior {
  50.  
  51.     public Text chatbox;
  52.     public InputField input;
  53.     public List<string> messages = new List<string>();
  54.  
  55.  
  56.     public void SendMessage(string message) {
  57.         RPC("SendMessageRPC", message);
  58.         //SendMessageRPC(message);
  59.     }
  60.    
  61.  
  62.     [BRPC]
  63.     private void SendMessageRPC(string message) {
  64.         Debug.Log("Sending message: " + message);
  65.         messages.Add("\n [] : " + message);
  66.         Debug.Log(messages.Count);
  67.         if (messages.Count > 50) {
  68.             messages.RemoveAt(0);
  69.         }
  70.         UpdateChatbox();
  71.     }
  72.  
  73.     private void UpdateChatbox() {
  74.        string text = "";
  75.        for(int i = messages.Count-1; i>= 0; i--){
  76.            text += messages[i];
  77.        }
  78.        chatbox.text = text;
  79.     }
  80.  
  81.     private void Update() {
  82.         base.Update();
  83.         if (Input.GetKeyDown(KeyCode.Return)) {
  84.             if (input.text != "") {
  85.                 SendMessage(input.text.Trim());
  86.             }
  87.             input.text = "";
  88.         }
  89.     }
  90. }
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