using UnityEngine;
using System.Collections;
public class LocalServer : MonoBehaviour
{
// Use this for initialization
void Start()
{
MasterServer.ipAddress = "192.168.1.112";
MasterServer.port = 23466;
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(0, 20, 150, 20), "StartLocalServer"))
{
Network.InitializeServer(32, 25003,!Network.HavePublicAddress());
MasterServer.RegisterHost("MyGame1", "Game2", "test1");
}
}
}
using UnityEngine;
using System.Collections;
public class TestClient : MonoBehaviour
{
void Awake()
{
//master server ip
MasterServer.ipAddress = "192.168.1.112";
//port
MasterServer.port = 23466;
//请求主机列表 游戏类型名
MasterServer.RequestHostList("MyGame1");
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
HostData[] data = MasterServer.PollHostList();
foreach (HostData hostData in data)
{
GUILayout.BeginHorizontal();
string name = hostData.gameName + " " + hostData.connectedPlayers + " " + hostData.playerLimit;
GUILayout.Label(name);
GUILayout.Space(5);
string hostInfo = "";
foreach (string hostIp in hostData.ip)
{
hostInfo += hostIp + ":" + hostData.port + "\n";
}
GUILayout.Label(hostInfo);
GUILayout.Space(5);
GUILayout.FlexibleSpace();
if(GUILayout.Button("Connect"))
{
Network.Connect(hostData);
}
GUILayout.EndHorizontal();
}
}
}