1

我正在尝试使用 DOTsSample Unity 项目并希望使用此 NetworkStatistics 类(https://github.com/Unity-Technologies/DOTSSample/blob/master/Assets/Scripts/Game/Main/NetworkStatisticsClient.cs)并尝试编写一个脚本来显示我游戏中的网络统计信息,但问题是它总是返回 0。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetStats : MonoBehaviour
{
    // Start is called before the first frame update


    NetworkStatisticsClient networkStatisticsClient;
    private SocketTransport m_Transport;
    private NetworkClient m_NetworkClient;

    void Start()
    {

        m_Transport = new SocketTransport();
        m_Transport.Connect("127.0.0.1",80);

        m_NetworkClient = new NetworkClient(m_Transport);

        if (Application.isEditor || Game.game.buildId == "AutoBuild")
            NetworkClient.clientVerifyProtocol.Value = "0";

        m_NetworkClient.UpdateClientConfig();
        networkStatisticsClient = new NetworkStatisticsClient(m_NetworkClient);


    }

    // Update is called once per frame
    void Update()
    {
        //Client isConnected is False thus, the connection is not establishment is not correct
        Debug.Log("Rtt: " +  m_NetworkClient.isConnected + networkStatisticsClient.rtt.average);
    }
}

有人可以帮我吗?

4

1 回答 1

0

根据文档,我希望您在启动函数中的某个位置连接到服务器。

https://docs.unity3d.com/2017.3/Documentation/ScriptReference/Networking.NetworkClient.Connect.html

    public void Start()
    {
        myClient = NetworkClient.Instance;

        myClient.RegisterHandler(MsgType.SYSTEM_CONNECT, OnConnected);
        myClient.RegisterHandler(MsgType.SYSTEM_DISCONNECT, OnDisconnected);
        myClient.RegisterHandler(MsgType.SYSTEM_ERROR, OnError);
        // this line here is important
        myClient.Connect("127.0.0.1", 8888);
    }

网络客户端用于对服务器进行 rpc 调用。它不是医学上抽象所有网络连接的通用客户端。

于 2022-01-08T12:44:58.470 回答