Advertisement
Guest User

NetworkRPC

a guest
Feb 24th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Networking : MonoBehaviour {
  6.  
  7.     public string ipAddress = "127.0.0.1";
  8.     public int port = 25167;
  9.     public int maxConnections = 10;
  10.  
  11.     //public GameObject GameController;
  12.  
  13.     //for chatting
  14.     public List<string> chatLog = new List<string>();
  15.     public string currentMessage = "";
  16.  
  17.     public string playername = "default";
  18.  
  19.     // Use this for initialization
  20.     void Start () {
  21.         Application.runInBackground = true;
  22.    
  23.     }
  24.    
  25.     // Update is called once per frame
  26.     void Update () {
  27.     }
  28.  
  29.     void OnGUI(){
  30.         if (Network.peerType == NetworkPeerType.Disconnected) {
  31.             //ip text box
  32.             GUI.BeginGroup (new Rect (10, 10, 800, 600));
  33.             GUI.Label(new Rect(0.0f, 0.0f, 100, 25), "IP: ");
  34.             ipAddress = GUI.TextField(new Rect(40.0f, 0.0f, 100, 25), ipAddress);
  35.             //port text box
  36.             GUI.Label(new Rect(0.0f, 30.0f, 100, 25), "Port: ");
  37.             string tempport;
  38.             tempport = GUI.TextField(new Rect(40.0f, 30.0f, 100, 25), port.ToString());
  39.             port = int.Parse(tempport);
  40.             //player name text box
  41.             GUI.Label(new Rect(0.0f, 60.0f, 100, 25), "Name: ");
  42.             playername = GUI.TextField(new Rect(40.0f, 60.0f, 100, 25), playername);
  43.  
  44.  
  45.             //connect button
  46.             if (GUI.Button(new Rect(0.0f, 90.0f, 125, 25),"Connect")){
  47.                 print ("Connecting to " + ipAddress + " : " + port.ToString());
  48.                 Network.Connect(ipAddress, port);
  49.                 StartCoroutine (SendJoinMessage ());
  50.                 //StartCoroutine(OnConnect ());
  51.             }
  52.             //host server button
  53.             if (GUI.Button(new Rect(0.0f, 120.0f, 125, 25),"Host")){
  54.                 print ("Hosting server on " + ipAddress + " : " + port.ToString());
  55.                 Network.InitializeServer(maxConnections, port, false);
  56.                 //StartCoroutine(OnConnect ());
  57.             }
  58.             GUI.EndGroup ();
  59.         }
  60.         else{
  61.             GUI.BeginGroup (new Rect (10, 10, 800, 600));
  62.             //disconnect button
  63.             if (GUI.Button (new Rect(0.0f, 0.0f, 125, 25), "Disconnect")){
  64.                 Network.Disconnect(200);
  65.             }
  66.             //display server info
  67.             GUI.Label(new Rect(0.0f, 30.0f, 100, 25), "IP: " + ipAddress);
  68.             GUI.Label(new Rect(0.0f, 50.0f, 100, 25), "Port: " + port);
  69.             GUI.Label(new Rect(0.0f, 70.0f, 200, 25), "Players: " + (Network.connections.Length + 1));
  70.  
  71.             //chat input
  72.             GUI.SetNextControlName("chatfield");
  73.             currentMessage = GUI.TextField(new Rect(0.0f, Screen.height - 45, 200, 25), currentMessage);
  74.  
  75.             if (GUI.Button(new Rect(205, Screen.height - 45, 50, 25), "Send")){
  76.                 if (currentMessage.Length > 0){
  77.                     string temp = "[" + playername + "]: " + currentMessage;
  78.                     networkView.RPC ("Chat", RPCMode.All, temp);
  79.                     currentMessage = "";
  80.                 }
  81.             }
  82.             if (Event.current.isKey && Event.current.keyCode == KeyCode.Return){
  83.                 if (GUI.GetNameOfFocusedControl() == "chatfield"){             
  84.                     if (currentMessage.Length > 0){
  85.                         string temp = "[" + playername + "]: " + currentMessage;
  86.                         networkView.RPC ("Chat", RPCMode.All, temp);
  87.                         currentMessage = "";
  88.                     }
  89.                 }
  90.                 else{
  91.                     GUI.FocusControl("chatfield");
  92.                 }
  93.  
  94.             }
  95.             //chat log
  96.             int chatindex = 0;
  97.             foreach(string msg in chatLog){
  98.                 ++chatindex;
  99.                 GUI.Label(new Rect(0.0f, Screen.height - 65 - 10 * (chatLog.Count - chatindex), Screen.width, 25), msg);
  100.             }
  101.  
  102.             GUI.EndGroup ();
  103.         }
  104.     }
  105.     void OnPlayerDisconnected(NetworkPlayer player){
  106.         Network.RemoveRPCs (player);
  107.         Network.DestroyPlayerObjects (player);
  108.     }
  109.  
  110.     [RPC]      
  111.     void Chat(string message){
  112.         chatLog.Add (message);
  113.     }
  114.  
  115.     IEnumerator SendJoinMessage(){
  116.         yield return new WaitForSeconds (1);
  117.         if (Network.peerType == NetworkPeerType.Connecting) {
  118.             StartCoroutine (SendJoinMessage ());
  119.         }
  120.         else {
  121.             networkView.RPC ("Chat", RPCMode.All, playername + " has joined the server");
  122.         }
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement