只是设置一个初步测试,以便通过局域网获得一些基本网络,我遇到了问题。我根据bool
设置(host = true/false
)编写了一个充当服务器或客户端的脚本。我已经构建了服务器应用程序并将其放在一个静态IP为192.168.1.160
的系统上。我在Unity Editor中运行我的客户端,我收到错误:
'ClientDisconnected由于错误:超时UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()'
我在剧本中做错了什么?非常感谢你看到这个!
public class NetworkController : NetworkManager {
public static NetworkController netCtrl = null; // create singleton
public bool host = false;
private int hostPort = 7777;
private string hostIP = "192.168.1.160";
private bool useNat = true;
private string networkConnections;
private bool serverLaunched;
private bool clientLaunched;
private int maxPlayers = 32;
private void Awake()
{
InitNetworkController();
}
void InitNetworkController()
{
if (netCtrl == null)
netCtrl = this;
else if (netCtrl != null)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
if (host && !Network.isServer)
{
// setup server
LaunchServer();
}
if (!host && !Network.isClient)
{
// setup client
LaunchClient();
}
}
void LaunchServer()
{
Debug.Log("Starting Server");
Network.InitializeServer(32, hostPort, useNat);
}
void LaunchClient()
{
Debug.Log("Starting Client");
Network.Connect(hostIP, hostPort);
}
void OnGUI()
{
if (Network.isServer)
{
GUILayout.Label("Running as a server.");
InvokeRepeating("CheckForConnections", 1f, 1f);
GUILayout.Label("There are " + networkConnections + " client(s) connected");
}
else if (Network.isClient)
{
GUILayout.Label("Running as a client.");
}
else
{
GUILayout.Label("We have a problem!");
}
}
void CheckForConnections()
{
networkConnections = NetworkServer.connections.Count.ToString();
}
}