[我在C#中工作]为了确定天气,一台设备应成为具有本地客户端或仅具有客户端的服务器(用于无主服务器的网络),我需要先确定该设备是否还有其他可用服务器,然后再转换为具有本地客户端的服务器,如果不允许其他客户端设备通过接收来自服务器的NetworkDiscovery广播来加入-当前,我无法让客户端接收来自服务器的广播。我已经用脚本创建了两个空的游戏对象,一个脚本使它成为服务器,并且应该通过NetworkDiscovery从其NetworkDiscovery.StartAsServer()广播,但是目前我的NetworkDiscovery设置为NetworkDiscovery.StartAsClient()的客户端无法获取函数被调用,因此不接收来自服务器的广播。下面显示的两个脚本是客户端和服务器:客户-using UnityEngine;using System.Collections;using UnityEngine.Networking;public class SearchForServers : MonoBehaviour { bool serverIsFound; NetworkDiscovery NetworkDiscovery; // Use this for initialization void Start () { serverIsFound = false; NetworkClient Client = new NetworkClient(); NetworkDiscovery = gameObject.AddComponent<NetworkDiscovery>(); NetworkDiscovery.Initialize(); NetworkDiscovery.StartAsClient(); } void OnRecievedBroadcast(string fromAdress, string data) { serverIsFound = true; } // Update is called once per frame void Update () { Debug.Log("Server Found = " + serverIsFound); }}服务器- using UnityEngine;using System.Collections;using UnityEngine.Networking;public class CreateServer : MonoBehaviour{ bool create; int minPort = 10000; int maxPort = 10010; int defaultPort = 10000; NetworkDiscovery NetworkDiscovery; void Start() { create = true; if (create == true) { int serverPort = createServer(); if (serverPort != -1) { Debug.Log("Server created on port : " + serverPort); } else { Debug.Log("Failed to create Server"); } } NetworkDiscovery = gameObject.AddComponent<NetworkDiscovery>(); NetworkDiscovery.Initialize(); NetworkDiscovery.StartAsServer(); } //Creates a server then returns the port the server is created with. Returns -1 if server is not created int createServer() { int serverPort = -1; //Connect to default port bool serverCreated = NetworkServer.Listen(defaultPort); if (serverCreated) { serverPort = defaultPort; Debug.Log("Server Created with default port"); } else { Debug.Log("Failed to create with the default port"); //Try to create server with other port from min to max except the default port which we trid already for (int tempPort = minPort; tempPort <= maxPort; tempPort++) { //Skip the default port since we have already tried it if (tempPort != defaultPort) { //Exit loop if successfully create a server if (NetworkServer.Listen(tempPort)) { serverPort = tempPort; break; } //If this is the max port and server is not still created, show, failed to create server error if (tempPort == maxPort) { Debug.LogError("Failed to create server"); } } } } return serverPort; }}这些是带有调试日志记录的控制台日志感谢您的阅读,我希望您能意识到我出了问题的地方,或者实际上是如何完成上述任务。 @程序员 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 首先,您为每个服务器和客户端脚本命名相同的名称。您不需要服务器代码中的OnRecievedBroadcast。您只需要在客户端代码中使用它。客户端上未调用OnRecievedBroadcast,因为您没有覆盖NetworkDiscovery。您必须覆盖它。这是一个简单的发现代码:客户:public class NetClient : NetworkDiscovery{ void Start() { startClient(); } public void startClient() { Initialize(); StartAsClient(); } public override void OnReceivedBroadcast(string fromAddress, string data) { Debug.Log("Server Found"); }}服务器:public class NetServer : NetworkDiscovery{ // Use this for initialization void Start() { Application.runInBackground = true; startServer(); } //Call to create a server public void startServer() { int serverPort = createServer(); if (serverPort != -1) { Debug.Log("Server created on port : " + serverPort); broadcastData = serverPort.ToString(); Initialize(); StartAsServer(); } else { Debug.Log("Failed to create Server"); } }int minPort = 10000;int maxPort = 10010;int defaultPort = 10000;//Creates a server then returns the port the server is created with. Returns -1 if server is not createdprivate int createServer(){ int serverPort = -1; //Connect to default port bool serverCreated = NetworkServer.Listen(defaultPort); if (serverCreated) { serverPort = defaultPort; Debug.Log("Server Created with deafault port"); } else { Debug.Log("Failed to create with the default port"); //Try to create server with other port from min to max except the default port which we trid already for (int tempPort = minPort; tempPort <= maxPort; tempPort++) { //Skip the default port since we have already tried it if (tempPort != defaultPort) { //Exit loop if successfully create a server if (NetworkServer.Listen(tempPort)) { serverPort = tempPort; break; } //If this is the max port and server is not still created, show, failed to create server error if (tempPort == maxPort) { Debug.LogError("Failed to create server"); } } } } return serverPort;}}createServer()函数是您上一个问题中的函数。我认为您无法在编辑器中同时运行服务器和客户端代码。为了进行测试,您必须在启用了服务器代码/ NetServer的PC中构建代码。运行生成的程序,然后从编辑器中运行client / NetClient代码。这应该在服务器上发现游戏。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-04 16:20