我将为Unity游戏项目开发一个简单的多人游戏。我从HLAPI开始,但它似乎在产生和同步状态方面很慢,所以我决定从https://docs.unity3d.com/Manual/UNetInternetServicesOverview.html的基本示例开始调查Unity Internet服务。 这里的问题是过时的引用,所以我不确定我的麻烦的原因是我的误解还是错误的框架。(
无论如何,我拿了第一个例子,开始修改它,无法理解服务器的“连接”和“客户端”之间的对应关系。
即,
networkMatch.CreateMatch
方法创建了一个匹配项。OnMatchCreate
回调中,我检查NetworkServer.connections.Count
的值 - 它是0。networkMatch.ListMatches
并在回调中看到已创建匹配的matchInfoSnapshot.currentSize
- 它是1. NetworkServer.connections.Count
- 它仍然是0(!!!)。我检查NetworkServer.localConnections.Count
- 它也是0。好的,我假设本地客户端没有与服务器建立任何连接(???),让我们检查一下。
networkMatch.JoinMatch
- 它成功了。networkMatch.ListMatches
并在回调中看到我的匹配的matchInfoSnapshot.currentSize
- 现在是2。NetworkServer.connections.Count
- 它现在也是2(!!!)(而NetworkServer.localConnections.Count
仍为0)。所以,问题很简单 - 我希望不添加任何愚蠢的计数器变量,但...... 如何获得服务器上连接客户端的实际数量?
P.S:简化的测试脚本:
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
using UnityEngine.Networking.Match;
using System.Collections.Generic;
public class TestNetworkController : MonoBehaviour
{
private NetworkMatch networkMatch;
void Start () {
networkMatch = gameObject.AddComponent<NetworkMatch>();
networkMatch.CreateMatch ("TestMatch", 4, true, "", "", "", 0, 1, OnMatchCreate);
}
private void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfoResponse) {
NetworkServer.RegisterHandler (MsgType.Connect, OnServerConnect);
if (!NetworkServer.Listen (matchInfoResponse, 9000)) {
success = false;
}
if (success) {
Debug.Log ("Match created, connections="+NetworkServer.connections.Count+" localConnections="+NetworkServer.localConnections.Count);
networkMatch.ListMatches(0, 20, "", true, 0, 1, OnMatchesListPage1);
}
}
private void OnMatchesListPage1(bool success, string extendedInfo, List<MatchInfoSnapshot> matchListResponse) {
Debug.Log ("Match list received, connections=" + NetworkServer.connections.Count + " localConnections=" + NetworkServer.localConnections.Count);
Debug.Log ("Match 0 size "+matchListResponse[0].currentSize);
networkMatch.JoinMatch (matchListResponse[0].networkId, "", "", "", 0, 1, OnMatchJoined);
}
private void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfoResponse) {
if (success) {
NetworkClient networkClient = new NetworkClient ();
networkClient.RegisterHandler (MsgType.Connect, OnClientConnect);
networkClient.Connect (matchInfoResponse);
Debug.Log ("Match 0 joined, connections=" + NetworkServer.connections.Count + " localConnections=" + NetworkServer.localConnections.Count);
}
}
private void OnServerConnect(NetworkMessage networkMessage) {
Debug.Log("Client connected, connections="+NetworkServer.connections.Count);
networkMatch.ListMatches(0, 20, "", true, 0, 1, OnMatchesListPage2);
}
private void OnClientConnect(NetworkMessage networkMessage) {
Debug.Log("Connected to server, connections="+NetworkServer.connections.Count);
}
private void OnMatchesListPage2(bool success, string extendedInfo, List<MatchInfoSnapshot> matchListResponse) {
Debug.Log ("Match list received, connections=" + NetworkServer.connections.Count + " localConnections=" + NetworkServer.localConnections.Count);
Debug.Log ("Match 0 size "+matchListResponse[0].currentSize);
}
}
答案 0 :(得分:0)
您可以使用NetworkServer.numPlayers
但是,NetworkServer仅用于服务器端。如果您需要您的客户知道有多少其他客户,您必须同步一个变量。
如果您是客户,请使用NetworkClient.allClients获取与服务器的连接。