我将为我的 Unity 游戏项目开发一个简单的多人游戏。我从 HLAPI 开始,但它在生成和同步状态时似乎非常慢,所以我决定从https://docs.unity3d.com/Manual/上的基本示例开始研究 Unity Internet 服务。 UNetInternetServicesOverview.html。这里的问题在于过时的参考资料,所以我不确定我的麻烦的原因是我的误解还是错误的框架。(
反正我拿了第一个例子,开始修改它,无法理解服务器的“连接”和“客户端”之间的对应关系。
即,
- 我确实用
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)。
所以,问题很简单——我不希望添加任何愚蠢的计数器变量,但是...... 我怎样才能获得服务器上连接客户端的实际数量?
PS:简化的测试脚本:
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);
}
}