即使我正在运行NetworkTransport.Init(),也会获得Network Init NullReferenceException

时间:2017-10-18 15:30:01

标签: c# unity3d networking

我在这里弄错了但不确定它是怎么回事。我收到了错误:

  

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);
    }
}

2 个答案:

答案 0 :(得分:0)

port之前设置NetworkTransport.Init()喜欢:

  • NetworkManager.singleton.networkPort = 7777;

  • NetworkTransport.Init();

答案 1 :(得分:0)

对我来说,问题发生在我在Initialize()的{​​{1}}上调用StartAsClient()NetworkDiscovery时。解决方案是等待一秒钟然后调用这些方法。