Advertisement
Guest User

Menu

a guest
Feb 8th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Net;
  5. using TNet;
  6.  
  7. public class Menu : MonoBehaviour
  8. {
  9.  
  10.     public string GameLevel = "Level";
  11.  
  12.     void Awake()
  13.     {
  14.  
  15.     }
  16.  
  17.     void Start()
  18.     {
  19.         if (Application.isPlaying)
  20.         {
  21.             TNManager.Connect("213.144.22.94");
  22.  
  23.             // Make it possible to use UDP using a random port
  24.             TNManager.StartUDP(Random.Range(10000, 50000));
  25.  
  26.             InvokeRepeating("Refresh", 2, 2);            
  27.         }
  28.          
  29.     }
  30.  
  31.     void OnGUI()
  32.     {
  33.         if (TNManager.isConnected)
  34.         {
  35.             if (GUILayout.Button("Join Random Channel"))
  36.             {
  37.                 TNManager.JoinRandomChannel(GameLevel, false, 8, "");
  38.             }
  39.  
  40.             if (GUILayout.Button("Get Channel List"))
  41.             {
  42.                 TNManager.client.BeginSend(Packet.RequestChannelList);
  43.                 TNManager.client.EndSend();
  44.             }
  45.         }
  46.     }
  47.  
  48.     /// <summary>
  49.     /// We connected to the GameServer
  50.     /// </summary>
  51.     void OnNetworkConnect(bool success, string message)
  52.     {
  53.         Debug.Log("OnNetworkConnect: " + success + message);
  54.         TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;
  55.         TNManager.StartUDP(Random.Range(10000, 50000)); // Opened UDP port on my server/router
  56.     }
  57.  
  58.     /// <summary>
  59.     /// Give us the Channellist
  60.     /// </summary>
  61.     void OnChannelList(Packet response, BinaryReader reader, IPEndPoint source)
  62.     {
  63.         int channels = reader.ReadInt32();
  64.  
  65.         // Read all incoming channel data
  66.         for (int i = 0; i < channels; ++i)
  67.         {
  68.             int id = reader.ReadInt32();
  69.             int players = reader.ReadUInt16();
  70.             int limit = reader.ReadUInt16();
  71.             bool pass = reader.ReadBoolean();
  72.             bool persistent = reader.ReadBoolean();
  73.             string level = reader.ReadString();
  74.             string data = reader.ReadString();
  75.  
  76.             if (GUI.Button(new Rect(300, 20, 300, 30), "Server: " + level + " Player Count: " + players + " Player Limit: " + limit))
  77.             {
  78.  
  79.             }
  80.             Debug.Log("channel ID: " + id + " Player Count: " + players + " limit: " + limit + " pass: " + pass + " persistent: " + persistent + " level: " + level + " data: " + data);
  81.  
  82.         }
  83.     }
  84.  
  85.     /// <summary>
  86.     /// Refresh the Channellist
  87.     /// </summary>
  88.     void Refresh()
  89.     {
  90.         TNManager.client.BeginSend(Packet.RequestChannelList);
  91.         TNManager.client.EndSend();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement