Unity3d, NAT client sever, and list all servers

February 13, 2013   ·   0 Comments

Screen Shot 2013-02-13 at 12.17.52 PM

Screen Shot 2013-02-13 at 12.17.52 PM

using UnityEngine;
using System.Collections;

public class networkClass : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	
//	private bool isClientWorking=false;
	
	void OnGUI() {
		
			
		/// server  code
		if (GUILayout.Button ("Start Server")) {
			        // Use NAT punchthrough if no public IP present
			        var useNat = !Network.HavePublicAddress();
			        Network.InitializeServer(32, 25002, useNat);
			        MasterServer.RegisterHost("PowenKoGame", 
			            "PowenKo Server1", "l33t game for all");
    	}
		
		
		
		/// client code
		
		if (GUILayout.Button ("Start Client"))
		{
				
			MasterServer.ClearHostList();
	        MasterServer.RequestHostList("MyUniqueGameType");		
		
		}
		

		
		
        if (MasterServer.PollHostList().Length != 0) {
				HostData[]  hostData  = MasterServer.PollHostList();
			// Go through all the hosts in the host list
			int i=0;
			while (i < hostData.Length) {
			{
				HostData element=hostData[i];
				GUILayout.BeginHorizontal();	
				string name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
				GUILayout.Label(name);	
				GUILayout.Space(5);
				string hostInfo = "[";
				//for (var host in element.ip){
				int j=0;
					int t_len1=element.ip.Length;
				while (i <t_len1 ) {		
					string host=	element.ip[j];
					hostInfo = hostInfo + host + ":" + element.port + " ";
					i++;
				}
				hostInfo = hostInfo + "]";
				GUILayout.Label(hostInfo);	
				GUILayout.Space(5);
				GUILayout.Label(element.comment);
				GUILayout.Space(5);
				GUILayout.FlexibleSpace();
				if (GUILayout.Button("Connect to"+element.gameName))
				{
					// Connect to HostData struct, internally the correct method is used (GUID when using NAT).
					Network.Connect(element);			
				}
				GUILayout.EndHorizontal();	
					  i++;
			} 
	}
	
	
}
	}
}

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon