string serverAddress;
int serverPort = 5128;
void Start ()
{
// i'm hosting the server on the same PC i'm running the client on
// otherwise set the IP of your server here
serverAddress = TNet.Tools.localAddress.ToString();
TNManager.client.onSetAdmin += OnSetAdmin;
}
// GUI button handler
public void OnClick(string sender)
{
if (sender == "btnJoinServer")
{
// connect to server
// declare int serverPort elsewhere and set it to your server's port
Debug.Log("Connecting to " + serverAddress + ":" + serverPort.ToString());
TNManager.Connect(serverAddress, serverPort);
}
else if (sender == "btnVerifyAdmin")
{
if (TNManager.isConnected)
TNManager.SetAdmin("password123");
else
Debug.LogError("Connect to the server before calling SetAdmin");
}
}
void OnNetworkConnect (bool result, string message)
{
if (result)
{
Debug.Log("connected to server");
// Make it possible for this client to use UDP using a random port
TNManager.StartUDP(Random.Range(10000, 50000));
}
else
{
Debug.LogError(message);
}
}
void OnSetAdmin(Player admin)
{
Debug.Log(admin.name + " has logged in as admin");
}