【发布时间】:2017-01-28 16:42:46
【问题描述】:
我将为我的 Unity 游戏项目开发一个简单的多人游戏。我从 HLAPI 开始,但它在生成和同步状态方面似乎非常慢,所以我决定改为研究 Unity Internet 服务,从https://docs.unity3d.com/Manual/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)。
所以,问题很简单——我不想添加任何愚蠢的计数器变量,但是...... 如何获取服务器上连接客户端的实际数量?
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);
}
}
【问题讨论】:
-
您确定您没有两次调用
JoinMatch吗?找出答案的唯一方法是您是否在问题中添加了编辑并发布了您用于执行此测试的代码。 -
@Programmer,绝对确定。我添加了一个简单的脚本,以便您检查。
标签: c# networking unity3d unity5 unity3d-unet