Assets\mqtthandler.cs(59,26):错误 CS7069:对类型 'IPAddress' 的引用声称它在 'System' 中定义,但找不到

Assets\mqtthandler.cs(59,26): error CS7069: Reference to type 'IPAddress' claims it is defined in 'System', but it could not be found

我使用 Unity 中的 M2MQTT 库为 Microsoft Hololens 创建了一个 MQTT 客户端,但我无法构建 Unity 项目,出现错误 "Assets\mqtthandler.cs(59,26): error CS7069: Reference to type 'IPAddress' claims it is defined in 'System', but it could not be found"。

我需要构建它以将其部署到 Hololens 模拟器。

在我的脚本中 mqtthandler.cs 我的代码中的 brokerHostname 在尝试创建 MQTTClient 时以某种方式引发了此错误。 Visual Studio 在此脚本中没有看到任何错误,只有当我尝试在 Unity 中构建我的项目时,我在 Unity 控制台中才收到此错误。

如果我 运行 我在 Unity 中的项目本身我可以成功接收 mqtt 消息。

我已经重新导入了所有资产,清除了 Unity 缓存,重新生成了 M2MQTT.NET.dll。

我正在使用 Unity 2018.3 和 Visual Studio 2019。

我的代码是:

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


// including the M2Mqtt Library
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Exceptions;

using System.Net;
using System.Net.Sockets;



public class mqtthandler : MonoBehaviour
{
    private MqttClient client;
    // The connection information
    public string brokerHostname = "127.0.0.1";
    public int brokerPort = 1883;
    public string userName = "test";
    public string password = "test";
    public static string messageOutput;
    public static string topicOutput;
    //public TextAsset certificate;
    // listen on all the Topic
    static string subTopic = "#";

    // Start is called before the first frame update
    void Start()
    {
        if (brokerHostname != null && userName != null && password !=null)
        {
            Debug.Log("connecting to " + brokerHostname + ":" + brokerPort);
            Connect();
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE };
            client.Subscribe(new string[] { subTopic }, qosLevels);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void Connect()
    {
        Debug.Log("about to connect on '" + brokerHostname + "'");
        // Forming a certificate based on a TextAsset
        /*X509Certificate cert = new X509Certificate();
        cert.Import(certificate.bytes);
        Debug.Log("Using the certificate '" + cert + "'");*/
        client = new MqttClient(brokerHostname/*, brokerPort, false, null, true, cert, null, MqttSslProtocols.TLSv1_0, MyRemoteCertificateValidationCallback*/);
        string clientId = Guid.NewGuid().ToString();
        Debug.Log("About to connect using '" + userName + "' / '" + password + "'");
        try
        {
            client.Connect(clientId, userName, password);
        }
        catch (Exception e)
        {
            Debug.LogError("Connection error: " + e);
        }
    }

    void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        string msg = System.Text.Encoding.UTF8.GetString(e.Message);
        Debug.Log("Received message from " + e.Topic + " : " + msg);
        messageOutput = msg;
        topicOutput = e.Topic;
    }

    private void Publish(string _topic, string msg)
    {
        client.Publish(
            _topic, Encoding.UTF8.GetBytes(msg),
            MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
    }
}

问题已解决:我必须构建 M2MQTT.WinRT.dll 并将其也导入到 Assets 文件夹中。现在我可以成功构建Unity项目了。