- Home /
Question by
rocket350 · Mar 21, 2014 at 08:24 AM ·
c#convertmasterserver
Master Server example in c#!
In the Reference Manual, there is a example script for connecting to the master server.
But it is in javascript. Dose anybody know the c# version.
PS: I bet I missed some view in c# option, so I'm sorry for the silly question.
Comment
Best Answer
Answer by pako · Mar 21, 2014 at 03:13 PM
I've translated the javascript example to C# for you:
A server might be started with code similar to this:-
void OnGUI() {
if (GUILayout.Button ("Start Server"))
{
// Use NAT punchthrough if no public IP present
Network.InitializeServer(32, 25002, !Network.HavePublicAddress());
MasterServer.RegisterHost("MyUniqueGameType", "JohnDoes game", "l33t game for all");
}
The connection routine might look something like this:
void OnGUI() {
HostData[] data = MasterServer.PollHostList();
// Go through all the hosts in the host list
foreach (var element in data)
{
GUILayout.BeginHorizontal();
var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
GUILayout.Label(name);
GUILayout.Space(5);
var hostInfo;
hostInfo = "[";
foreach (var host in element.ip)
hostInfo = hostInfo + host + ":" + element.port + " ";
hostInfo = hostInfo + "]";
GUILayout.Label(hostInfo);
GUILayout.Space(5);
GUILayout.Label(element.comment);
GUILayout.Space(5);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Connect"))
{
// Connect to HostData struct, internally the correct method is used (GUID when using NAT).
Network.Connect(element);
}
GUILayout.EndHorizontal();
}