【问题标题】:Getting Network Init NullReferenceException even though I'm running NetworkTransport.Init()即使我正在运行 NetworkTransport.Init() 也获得 Network Init NullReferenceException
【发布时间】:2017-10-18 15:30:00
【问题描述】:

我这里出了点问题,但不知道怎么错了。我收到了错误:

NullReferenceException: 在使用该库之前,您应该调用 Init(),之后不要忘记调用 Shutdown()

Server.Update ()(位于 Assets/Scripts/Server.cs:69)

...即使我在 Start() 的第一行调用 NetworkTransport.Init();。我错过了什么?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Server : MonoBehaviour
{
    public Texture2D textureToSend;
    string messageToSend = "Test Message";

    private const int MAX_CONNECTION = 100;
    private int port = 5701;
    private int hostId;
    private int webHostId;
    private int reliableChannel;
    private int reliableSeqChannel;
    private int reliableFragChannel;
    private int unreliableChannel;
    private int unreliableSeqChannel;

    private bool isStarted = false;
    private byte error;

    private GameObject infoDisplayText;

    private void Start()
    {
        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.Reliable);
        reliableSeqChannel = cc.AddChannel(QosType.ReliableSequenced);
        reliableFragChannel = cc.AddChannel(QosType.ReliableFragmented);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);
        unreliableSeqChannel = cc.AddChannel(QosType.UnreliableSequenced);

        HostTopology topo = new HostTopology(cc, MAX_CONNECTION);

        hostId = NetworkTransport.AddHost(topo, port, null);

        if (NetworkTransport.IsStarted)
        {
            isStarted = true;
            Debug.Log("NetworkTransport is Started.");
            infoDisplayText.GetComponent<Text>().text += "NetworkTransport is Started.\n";
        }

        Debug.Log("Server Started.");
        infoDisplayText.GetComponent<Text>().text += "Server Started.\n";
    }

    private void Update()
    {
        if (!isStarted)
            return;

        int recHostId;
        int connectionId;
        int channelId;
        byte[] recBuffer = new byte[1024];
        int bufferSize = 1024;
        int dataSize;
        byte error;

        NetworkEventType recData = NetworkTransport.Receive
            (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
            case NetworkEventType.ConnectEvent:
                Debug.Log("Player " + connectionId + " has connected");
                infoDisplayText.GetComponent<Text>().text += "Player " + connectionId + " has connected\n";
                break;

            case NetworkEventType.DataEvent:
                break;

            case NetworkEventType.DisconnectEvent:
                break;
        }
    }

    public void SendOnButtonPress()
    {
        SendTexture(textureToSend, messageToSend);
    }

    //Call to send the Texture and a simple string message
    public void SendTexture(Texture2D texture, string message)
    {
        TextureMessage msg = new TextureMessage();

        //Convert Texture2D to byte array

        msg.textureBytes = texture.GetRawTextureData();
        msg.message = message;

        NetworkServer.SendToAll(MyMsgType.texture, msg);
    }
}

【问题讨论】:

  • Init() 没有在第一行被调用。
  • 您在哪一行代码中遇到了该错误? @RH6 NetworkTransport.Init() 正在被调用。它不必在第一行。必须在使用 Network API 之前调用
  • @Programmer 我在更新函数中的NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error); 行收到错误。这似乎很奇怪,因为在网络初始化和启动之前更新不会运行。 (更新直到 isStarted = true 才会运行)
  • 我在我的电脑上运行了这段代码,它工作正常。没有这样的错误
  • 这实际上是您的代码的样子还是您在发布之前对其进行了更改?

标签: c# unity3d networking


【解决方案1】:

NetworkTransport.Init()之前设置port 赞:

  • NetworkManager.singleton.networkPort = 7777;

  • NetworkTransport.Init();

【讨论】:

    【解决方案2】:

    对我来说,当我在NetworkDiscovery 上的Start() 上拨打Initialize()StartAsClient() 时,问题就发生了。解决方案就是稍等片刻,然后调用这些方法。

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 1970-01-01
      • 2013-03-06
      • 2022-10-05
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多